![]() ![]() |
![]() |
|
1module FctDef
2where
3
4import Prelude hiding ( splitAt
5 , recip
6 , abs
7 , signum
8 , not
9 , fst, snd
10 , null, head, tail
11 , const
12 , even
13 )
14
15import qualified Prelude as P
16
17-- Funktionsdefinitionen
18
19isDigit :: Char -> Bool
20isDigit c
21 = c >= '0' && c <= '9'
22
23even :: Integral a => a -> Bool
24even n
25 = n `mod` 2 == 0
26
27splitAt :: Int -> [a] -> ([a], [a])
28splitAt n xs
29 = (take n xs, drop n xs)
30
31recip :: Fractional a => a -> a
32recip x = 1 / x
33
34-- Verzweigungen
35
36abs n
37 = if n >= 0
38 then n
39 else -n
40
41signum n
42 = if n < 0
43 then -1
44 else if n == 0
45 then 0
46 else 1
47
48-- Waechter (guards)
49
50abs' n
51 | n >= 0 = n
52 | otherwise = -n
53
54signum' n
55 | n < 0 = -1
56 | n == 0 = 0
57 | n > 0 = 1
58
59-- Typen von abs, signum ?
60
61-- Mustervergleiche (pattern matching)
62
63not :: Bool -> Bool
64not x
65 = if x then False else True
66
67not' False = True
68not' True = False
69
70not'' False = True
71not'' _ = False
72
73(&&&) :: Bool -> Bool -> Bool
74False &&& False = False
75False &&& True = False
76True &&& False = False
77True &&& True = True
78
79(.&&.) :: Bool -> Bool -> Bool
80True .&&. True = True
81_ .&&. _ = False
82
83(<&&>) :: Bool -> Bool -> Bool
84True <&&> b = b
85_ <&&> _ = False
86
87(&&&&) :: Bool -> Bool -> Bool
88a &&&& b = if a
89 then b
90 else False
91
92fst (x, _) = x
93
94snd (_, y) = y
95
96-- Typen von fst und snd?
97
98test1 ['a', _, _] = True
99test1 _ = False
100
101-- Typ von test1?
102
103null :: [t] -> Bool
104null [] = True
105null _ = False
106
107head :: [t] -> t
108head (x : _) = x
109head [] = error "hahahahahah"
110
111tail :: [t] -> [t]
112tail (_ : xs) = xs
113
114-- lambda-Ausdruecke
115
116l1 = \ x -> x + x
117
118l2 = (\ x -> x + x) 2
119
120l3 = (\ x -> x + x) (2 + 3)
121
122l4 = \ x -> (\ y -> x + y)
123
124
125-- Funktionen als Resultat
126
127const :: a -> b -> a
128const c _x = c
129
130const' :: a -> (b -> a)
131const' c = \ _x -> c
132
133-- Funktionen als Argument
134
135odds :: Int -> [Int]
136odds n
137 = map f [0..n-1]
138 where
139 f x = 2 * x + 1
140
141-- :t map ?
142
143odds' n
144 = map (\ x -> 2 * x + 1) [0..n-1]
145
146-- :t odds' ?
147
148-- :t (1+)
149-- :t (*2)
150-- :t (== 1)
151-- :t (1 <)
|
Letzte Änderung: 24.11.2020 | © Prof. Dr. Uwe Schmidt![]() |