1.
[x^2| x<- [1..10],odd x]==[1,9,25,49,81]
[z| x <- [1..10],let z = x^2,z>50] # kann Abkürzung für oben
2.[(wert,name)| wert <- [1..3],name <- ['a'..'b']]
result:
[(1,'a'),(1,'b'),(2,'a'),(2,'b'),(3,'a'),(3,'b')]
it :: (Enum t, Num t) => [(t, Char)]
it :: (Enum t, Num t) => [(t, Char)]
3. type MyWeirdType = (Double,[(Bool,Integer)])
4.data MyTuple = MyTupelConstructor Int Int
##wie Tupel ohne Klammern und kommas;stattdessen mit vorangestellem Konstruktor
5. data Frucht = Apfel (Int,Double)
| Birne Int Double
| Banane Int Int Double
| Birne Int Double
| Banane Int Int Double
6.
data Frucht = Apfel {preis::Int,anzahl::Int}
| Birne {preis::Int,anzahl::Int}
|Banane {preis::Int,anzhal::Int
,krümmung:Double}
data Maybe a = Noting | Just a
data Frucht = Apfel {preis::Int,anzahl::Int}
| Birne {preis::Int,anzahl::Int}
|Banane {preis::Int,anzhal::Int
,krümmung:Double}
getKrümmung :: Frucht -> Maybe Double
getKrümmung Banane {krümmung=k} = Just k
getKrümmung _ = Nothing
getKrümmung Banane {krümmung=k} = Just k
getKrümmung _ = Nothing
1. Maybe
example :
data Maybe a = Nothing | Just a
isJust :: Maybe a -> Bool
isJust Nothing = False
isJust _ = True
isJust Nothing = False
isJust _ = True
2.Either
example:
Module Data.Either where
data Either a b = Left a | Right b
data Either a b = Left a | Right b
isRight :: Either a b -> Bool
isRight (Left _) = False
isRight (Right _) = True
isRight (Left _) = False
isRight (Right _) = True
lefts :: [Either a b]-> [a]
lefts x = [a | Left a <- x]
lefts x = [a | Left a <- x]
partitionEithers :: [Either a b]-> ([a],[b])
partitionEithers [] = ([],[])
partitionEithers (h:t)
| Left l <- h = (l:ls,rs)
| Right r <- h = (ls,r:rs)
where (ls,rs) = partitionEithers t
partitionEithers [] = ([],[])
partitionEithers (h:t)
| Left l <- h = (l:ls,rs)
| Right r <- h = (ls,r:rs)
where (ls,rs) = partitionEithers t
#just example
Funktionsdefinition
double1 x = x+x
foo x y z = x +y *double z
add = \x y -> x +y
foo x y z = x +y *double z
add = \x y -> x +y
twice f x = f x x
double2 y = twice (+) y
double3 = twice (+)
double2 y = twice (+) y
double3 = twice (+)
2.Höhepunkt
Int -> Int -> Int gleich as Int->(Int -> Int)
3.
drop :: Int -> [a]->[a]
drop n xs | n <= 0 = xs
drop _ [] = []
drop n (_:xs) = drop (n-1) xs
drop n xs | n <= 0 = xs
drop _ [] = []
drop n (_:xs) = drop (n-1) xs
drop 3 [1,2,3,4,5]
4.
Einrückung ersetzt Klammerung
betragSumme :: Int -> Int -> Int
betragSumme x y =
let x' = abs x
y' = case y of 0 -> 0
x| x>0 ->x
| otherwise -> -x
in x' + y'
betragSumme x y =
let x' = abs x
y' = case y of 0 -> 0
x| x>0 ->x
| otherwise -> -x
in x' + y'
5.
map :: (a->b)->[a]->[b] # (a->b) funktion
map f [] = []
map f (x:xs) =
f x : map f xs
map f [] = []
map f (x:xs) =
f x : map f xs