next up previous contents
Next: Funktionsdefinitionen Up: Grundlegende Sprachelemente Previous: Typen

Typen von Funktionen

Hier noch mal ausführliche Beispiele:

 ----------------POLYMORPHISM, list-type examples


Prelude> :type []
[] :: [a]
Prelude> :type (:)
(:) :: a -> [a] -> [a]
Prelude> :type 'a':[]
'a' : [] :: [Char]
Prelude> 'a':[]
"a"
Prelude> :type ['a','b','c']:[]
['a','b','c'] : [] :: [[Char]]
Prelude> ['a','b','c']:[]
["abc"]
Prelude> :type ['a','b','c']:['a','b','c']
ERROR: Type error in application
*** expression : ['a','b','c'] : ['a','b','c']
*** term : ['a','b','c']
*** type : [Char]
*** does not match : Char
Prelude> :type ['a','b','c']:[['a','b','c']]
['a','b','c'] : [['a','b','c']] :: [[Char]]
Prelude> ['a','b','c']:[['a','b','c']]
["abc", "abc"]
Prelude> :type length
length :: [a] -> Int
Prelude> length []
0
Prelude> length ['a']
1
Prelude> length [1000, 10000]
2
Prelude> length ( ['a','b','c']:[['a','b','c']])
2


----------HIGHER ORDER FUNCTION EXAMPLE


tabulate :: Float->Float->Int->(Float->Float)->[Float]
tabulate start step times f
|times <=0 = []
|otherwise = (f start):(tabulate (start+step) step (times-1) f)


Main> tabulate (-1) 0.2 11 sin
[-0.841471, -0.717356, -0.564642, -0.389418, -0.198669, -2.98023e-08,
0.198669, 0.389418, 0.564642, 0.717356, 0.841471]


Main> :type diff
diff :: Fractional a => (a -> a) -> a -> a
Main> :type (diff cos)
diff cos :: Floating a => a -> a
Main> tabulate (-1) 0.2 11 (diff cos)
[0.8412, 0.716984, 0.564218, 0.38898, 0.198126, -0.000476837, -0.199199,
-0.389874, -0.565052, -0.717699, -0.841737]


Main> let around_zero = tabulate (-1) 0.2 11 in around_zero sin
[-0.841471, -0.717356, -0.564642, -0.389418, -0.198669, -2.98023e-08,
0.198669, 0.389418, 0.564642, 0.717356, 0.841471]


next up previous contents
Next: Funktionsdefinitionen Up: Grundlegende Sprachelemente Previous: Typen
Ronald Blaschke
1998-04-19