Tutorium 5 und 7
f x = cos x / 2
f pi
g x = sin x
f x y = (x / sin x) * g y
myfunction x yf 2 2 -- Rufe f mit Parametern 2 und 2 auf
g (f 2 2) -- Rufe g mit Ergebnis von (f 2 2) als Parameter auf
Funktionen können auch Funktionen als Ein- oder Ausgabe haben
callWithPlus5 f = \x -> f (x+5)
invertNum x = -x
invertNum (-3)
(callWithPlus5 invertNum) (-3)
Eingabe der Funktion callWithPlus5: Funktion die Integer entgegennimmt
Ausgabe der Funktion callWithPlus5: Funktion die Integer entgegennimmt
Int
Bool
Float
Char
[1,2,3]
(1,2)
Int: div und modFloat: (/)Bool: (&&) und (||)h x y = x + y + 2
:t h
Hier relevant:
a -> a -> a
bzw.:
a -> (a -> a)
Parameter 1: Typ a
Parameter 2: Typ a
Ausgabe: Typ a
Zusätzlich bedeutet Num a, dass a die Typklasse Num hat.
Details dazu später
:t (||)
Abbildung von zwei Bool Eingaben auf eine Bool Ausgabe
...eine Funktion die als Eingabe nimmt:
IntegerBoolChar
und deren Ausgabe ein Float ist...eine Funktion die als Eingabe eine Funktion vom Typ Integer -> Bool nimmt und eine Funktion vom Typ Bool->Integer ausgibt?
Nervig:
add5 x = x + 5
add10 x = add5 (add5 x)
add10 10
add5 wird nur als Subroutine von add10 verwendet.
Besser:
add10' x = add5' (add5' x)
where add5' z = z + 5
add10' 10
absoluteVal0 x = if x>=0 then x else -x
absoluteVal0 (-10)
condInvert True a = -a
condInvert False a = a
condInvert True 10
condInvert False 10
absoluteVal1 x
| x >= 0 = x
| otherwise = -x
absoluteVal1 (-10)
absoluteVal1 x
| x > 0 = x
| x == 0 = error "For some (unknown) reason this is supposed to be impossible for 0"
| otherwise = -x
absoluteVal1 0
Klassisches Beispiel: Fakultät
fak 0 = 1
fak n = n * fak (n-1)
fak 6
Warum Endrekursion?
-- Aufgabe 1: Endrekursive Version von `fak` (2 min)
fak' n = fakAcc n 1
where fakAcc n acc
| n == 0 = acc
| otherwise = fakAcc (n-1) (n*acc)
fak' 5
-- Aufgabe 2: Endrekursion für Fibbonaci Zahlen (5 min)
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
fib' n = fibAcc n 0 1
where fibAcc n a b
| n==0 = a
| otherwise = fibAcc (n-1) b (a+b)
fib 30
fib' 30
Eine Liste ist entweder...
[](x:xs) aus einem Listenkopf x und einer Restliste xs-- Gib alle Zahlen kleiner gleich n als Liste zurück
mylist 0 = []
mylist n = n : mylist (n-1)
mylist 5
-- Aufgabe 3: Schreibe eine Funktion, die alle geraden Zahlen < n zurückgibt (3min)
evennumbers n acc
| n == 0 = acc ++ [0]
| n `mod` 2 == 0 = evennumbers (n-2) (acc ++ [n])
| n `mod` 2 == 1 = evennumbers (n-1) acc
evennumbers' n = tail (evennumbers'' n)
where evennumbers'' n
| n == 0 = [0]
| even n = n : (evennumbers'' (n-2))
| otherwise = evennumbers'' (n-1)
evennumbers' 10
(++) Konkatenation(!!) Indexzugriffhead, last Erstes bzw. letztes Elementnull ist Liste leer?take / drop Die ersten n Elemente nehmen/auslassenlength Länge der Listereverse Dreht die Liste umelem x l Testet, ob x in der Liste enthalten ist...sind eigentlich nur Listen von Chars
head "Text"
tail "Text"
<Name>.hsmodule <Name> where
...
Probleme? Fragen?
max3if x y z = if x >= y
then if x >= z then x else z
else if y >= z then y else z
max3guard x y z
| x >= y && x >= z = x
| y >= x && y >= z = y
| otherwise = z
max3max x y z = max x (max y z)
max3if 3 4 5
max3guard 3 4 5
max3max 3 4 5
-- Aufgabe 4 (10 min) baut die Listenfunktionen nach
concat' [] l2 = l2
concat' (x:xs) l2 = x : (concat' xs l2)
indexAccess 0 (x:xs) = x
indexAccess n [] = error "out of bounds"
indexAccess n (x:xs) = indexAccess (n-1) xs
head' [] = error "empty"
head' (x:xs) = x
last' [] = error "empty"
last' [x] = x
last' (x:xs) = last' xs
null' [] = True
null' l = False
take' 0 l = []
take' n [] = [] -- alternativ: error ""
take' n (x:xs) = x : take' (n-1) xs
drop' 0 l = l
drop' n [] = [] -- alternativ: error ""
drop' n (x:xs) = drop' (n-1) xs
reverse' l = reverseAcc l []
where
reverseAcc [] acc = acc
reverseAcc (x:xs) acc = reverseAcc xs (x:acc)
elem' x [] = False
elem' x (y:ys) = if x == y then True else elem' x ys
mytestlist = [0,1,2,3,4,5,6,7,8,9]
concat' [-2,-1] mytestlist
indexAccess 4 mytestlist
head' mytestlist
last' mytestlist
null' mytestlist
null' []
take' 3 mytestlist
drop' 3 mytestlist
reverse' mytestlist
elem' 3 mytestlist
elem' 10 mytestlist