-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathtypes_definisions.hs
More file actions
157 lines (113 loc) · 3.36 KB
/
types_definisions.hs
File metadata and controls
157 lines (113 loc) · 3.36 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
import Prelude hiding (Maybe, Just, Nothing)
type Pos = (Int, Int)
origin :: Pos
origin = (0, 0)
left :: Pos -> Pos
left (x, y) = (x-1, y)
type Pair a = (a, a)
mult :: Pair Int -> Int
mult (m, n) = m*n
copy :: a -> Pair a
copy x = (x, x)
type Trans = Pos -> Pos
left' :: Trans
left' (x, y) = (x-1, y)
--type - złączenie istniejących typów
--data - definicja zupełnie nowego typu
--
--data Bool = True | False
--True i False to constructors typu Bool
--
--type and constructor names must begin with an UPPERCASE letter
--functions and type variables begin with lowercase letter
data Answer = Yes | No | Unknown
answers :: [Answer]
answers = [Yes, No, Unknown]
flip :: Answer -> Answer
flip Yes = No
flip No = Yes
flip Unknown = Unknown
data Shape = Circle Float
| Rectangle Float Float
-- :t Circle :: Float -> Shape
-- :t Rectangle :: Float -> Float -> Shape
square :: Float -> Shape
square n = Rectangle n n
area :: Shape -> Float
area (Circle r) = pi * r * r
area (Rectangle a b) = a * b
data Maybe a = Nothing | Just a
safediv :: Int -> Int -> Maybe Int
safediv _ 0 = Nothing
safediv x y = Just (x `div` y)
safehead :: [a] -> Maybe a
safehead [] = Nothing
safehead xs = Just (head xs)
-- RECURSIVE TYPES
--natural number
data Nat = Zero | Succ Nat
deriving Show
--Zero :: Nat
--Succ :: Nat -> Nat
testNat = Succ (Succ (Succ Zero))
-- 3
-- 1 + (1 + (1 + 0))
nat2int :: Nat -> Int
nat2int Zero = 0
nat2Int (Succ n) = 1 + nat2int n
int2nat :: Int -> Nat
int2nat 0 = Zero
int2nat n = Succ (int2nat (n-1))
add :: Nat -> Nat -> Nat
add x y = int2nat (nat2int x + nat2int y)
add' :: Nat -> Nat -> Nat
add' Zero n = n
add' (Succ m) n = Succ (add m n)
testAdd = add (Succ (Succ Zero)) (Succ Zero)
data Expr = Val Int
| Add Expr Expr
| Mult Expr Expr
deriving Show
size :: Expr -> Int
size (Val _) = 1
size (Add a b) = size a + size b
size (Mult a b) = size a + size b
eval :: Expr -> Int
eval (Val n) = n
eval (Add a b) = eval a + eval b
eval (Mult a b) = eval a * eval b
fold :: (Int -> a) -> (a -> a -> a) -> (a -> a -> a) -> Expr -> a
fold fval fadd fmult expr = choice expr
where
choice (Val n) = fval n
choice (Add a b) = fadd (choice a) (choice b)
choice (Mult a b) = fmult (choice a) (choice b)
eval' = fold id (+) (*)
testEval = eval (Add (Val 1) (Mult (Val 2) (Val 3))) == 7
testEval' = eval' (Add (Val 1) (Mult (Val 2) (Val 3))) == 7
--binary trees with ints in nodes and leafs
data Tree = Leaf Int
| Node Tree Int Tree
deriving Show
tree = Node (Node (Leaf 1) 3 (Leaf 4))
5
(Node (Leaf 6) 7 (Leaf 9))
--check if an integer is in a given tree
contains :: Int -> Tree -> Bool
contains m (Leaf n) = m==n
contains m (Node left n right) = m==n
|| contains m left
|| contains m right
testContains = contains 5 tree
flatten :: Tree -> [Int]
flatten (Leaf m) = [m]
flatten (Node left m right) = flatten left ++ [m] ++ flatten right
--a tree is a search tree if flattened to a list it is sorted
testFlatten = flatten tree
--since our tree is a search tree, we can optimize the contains function
contains' :: Int -> Tree -> Bool
contains' m (Leaf n) = m==n
contains' m (Node left n right) | m==n = True
| m<n = contains m left
| m>n = contains m right
testContains' = contains' 5 tree