-----------------------Liste von Rechnern mit Aufstellungsort

type Room = String             --Typ-Synonyme fuer Lesbarkeit
type Hardware = String
type HwList = [(Hardware,Room)]

example_list =                                     --Beispiel
                [("PC","T01"),
                ("PC","Lab1"),("PC","Lab1"),("PC","Lab1"),
                ("PC","Lab2"),("Sun","Lab2"),("Sun","Lab2")]

                                               --Aufbau/Abbau
insert, delete:: Hardware->Room->HwList->HwList
insert h r hwl = (h,r):hwl
delete h r [] = []  --keine Wirkung wenn (h,r) nicht vorhanden
delete  h r ((h1,r1) : rest)
        |h1 == h && r1 == r = rest
        |otherwise          = (h1,r1) : delete h r rest 

                 --Funktionen fuer Anfragen, Aenderungen etc
locations:: Hardware->HwList->[Room]          --Ergebnis enthaelt Mehrfachvorkommen
locations h [] =[]
locations h ((h1,r1) : rest) 
        |h1 == h   = r1 : (locations h rest)
        |otherwise = locations h rest  

how_many:: Room->HwList->Int
how_many  r [] = 0
how_many r ((h1,r1) : rest) 
        |r1 == r   = 1 + how_many r rest 
        |otherwise = how_many r rest 

replace:: Hardware->Hardware->HwList->HwList
replace h h' [] = []
replace h h' ((h1,r1) : rest)  = entry : replace h h' rest
                          where entry |h1 == h   = (h',r1)
                                      |otherwise = (h1,r1)





-----------Alternativformulierung mit higher order functions

-------------------------------------------
is_wanted:: Hardware->(Hardware,Room)->Bool
is_wanted h (h',r) = h == h'

locations2:: Hardware->HwList->[Room]
locations2 h = map snd . filter (is_wanted h)
---------------------------------------------
               --dasselbe mit operator section
locations3:: Hardware->HwList->[Room]
locations3 h = map snd . filter ((== h).fst)
----------------------------------------------
----------------------------------------------
exchange::  Hardware->Hardware->(Hardware,Room)->(Hardware,Room)
exchange h h' (h'',r)
   |h == h'' = (h',r)
   |otherwise = (h'',r)

replace2:: Hardware->Hardware->HwList->HwList
replace2 h h' = map (exchange h h' )
--------------------------------------------------

--how_many : Hausaufgabe
  
