Grundlagen der Funktionalen Programmierung: Selbstdefinierte Datentypen |
|
|
|
1module TypeDecl
2where
3
4import Prelude hiding ( Left
5 , Right
6 )
7
8-- ----------------------------------------
9
10type Pos = (Int, Int)
11
12data Color
13 = Red | Green | Blue
14 deriving (Show)
15
16data Move
17 = Left | Right | Up | Down
18 deriving (Show)
19
20move :: Move -> Pos -> Pos
21move Left (x, y) = (x - 1, y )
22move Right (x, y) = (x + 1, y )
23move Up (x, y) = (x, y - 1)
24move Down (x, y) = (x, y + 1)
25
26moves :: [Move] -> Pos -> Pos
27moves ms p0 = foldl (\ p m -> move m p) p0 ms
28
29moves' :: [Move] -> Pos -> Pos
30moves' = flip (foldl (flip move))
31
32moves'' :: [Move] -> Pos -> Pos
33moves'' ms = foldl (>>>) id fs
34 where
35 fs = map move ms
36 (>>>) = flip (.)
37
38ms :: [Move]
39ms = [Down, Left, Down, Right, Up]
40
41ms0, ms0', ms0'' :: Pos
42ms0 = moves ms (0,0)
43ms0' = moves' ms (0,0)
44ms0'' = moves'' ms (0,0)
45
46-- ----------------------------------------
47
48data Shape
49 = Circle Float
50 | Rect Float Float
51 deriving (Show)
52
53square :: Float -> Shape
54square n = Rect n n
55
56area :: Shape -> Float
57area (Circle r) = pi * r * r
58area (Rect w h) = w * h
59
60-- ----------------------------------------
61
62safeDiv :: Int -> Int -> Maybe Int
63safeDiv _ 0 = Nothing
64safeDiv x y = Just (x `div` y)
65
66-- ----------------------------------------
67
68data Nat
69 = Zero
70 | Succ Nat
71 deriving (Show)
72
73intToNat :: Int -> Nat
74intToNat 0 = Zero
75intToNat n = Succ (intToNat (n-1))
76
77natToInt :: Nat -> Int
78natToInt Zero = 0
79natToInt (Succ n) = 1 + natToInt n
80
81add :: Nat -> Nat -> Nat
82add Zero n = n
83add (Succ m) n = Succ (add m n)
84
85sub :: Nat -> Nat -> Nat
86sub n Zero = n
87sub (Succ m) (Succ n) = sub m n
88sub _ _ = error "negative numbers not allowed"
89
90mul :: Nat -> Nat -> Nat
91mul Zero _n = Zero
92mul (Succ m) n = (m `mul` n) `add` n
93
94-- ----------------------------------------
95
96data List a
97 = Nil
98 | Cons a (List a)
99 deriving (Show)
100
101-- ----------------------------------------
102
103data IntTree
104 = Leaf
105 | Node IntTree Int IntTree
106 deriving (Show)
107
108t :: IntTree
109t = Node
110 (Node
111 (Node Leaf 1 Leaf)
112 3
113 (Node Leaf 4 Leaf)
114 )
115 5
116 (Node
117 (Node Leaf 6 Leaf)
118 7
119 (Node Leaf 9 Leaf)
120 )
121
122occurs :: Int -> IntTree -> Bool
123occurs _m Leaf
124 = False
125
126occurs m (Node l n r)
127 = m == n
128 ||
129 occurs m l
130 ||
131 occurs m r
132
133flatten :: IntTree -> [Int]
134flatten Leaf
135 = []
136flatten (Node l n r)
137 = flatten l ++ [n] ++ flatten r
138
139-- ----------------------------------------
140
141data Tree1 a
142 = Leaf1 a
143 | Node1 (Tree1 a) (Tree1 a)
144 deriving (Show)
145
146data Tree2 a
147 = Leaf2
148 | Node2 (Tree2 a) a (Tree2 a)
149 deriving (Show)
150
151data Tree3 a b
152 = Leaf3 a
153 | Node3 (Tree3 a b) b (Tree3 a b)
154 deriving (Show)
155
156data Tree4 a
157 = Node4 a [Tree4 a]
158 deriving (Show)
159
160-- ----------------------------------------
|
TypeDecl.hs |
Letzte Änderung: 15.12.2020 | © Prof. Dr. Uwe Schmidt |