forked from pietervdvn/Haskell
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathOplossingen1.hs
102 lines (70 loc) · 1.58 KB
/
Oplossingen1.hs
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
import Prelude (Int, Bool(True, False), String, (+), (*), (-))
-- Getting started
inc :: Int -> Int
inc i = i + 1
double :: Int -> Int
double i = i * 2
giveOne :: a -> Int
giveOne _ = 1
id :: a -> a
id a = a
const :: a -> b -> a
const a _ = a
-- Pattern matching
(&&) :: Bool -> Bool ->Bool
(&&) True True = True
(&&) _ _ = False
(||) :: Bool -> Bool -> Bool
(||) False False= False
(||) _ _ = True
not :: Bool -> Bool
not True = False
not False = True
-- Recursie
faculteit :: Int -> Int
faculteit 0 = 1
faculteit i = i * faculteit (i - 1)
fib :: Int -> Int
fib 0 = 1
fib 1 = 1
fib i = fib (i - 1) + fib (i - 2)
-- Higher order
doTwice :: (a -> a) -> a -> a
doTwice f a = f (f a)
infixr 9 $
($) :: (a -> b) -> a -> b
($) f a = f a
(.) :: (b -> c) -> (a -> b) -> a -> c
(.) f g a = f (g a)
flip :: (a -> b -> c) -> b -> a -> c
flip f b a = f a b
-- Lists
and :: [Bool] -> Bool
and [] = True
and (False:bs) = False
and (_:bs) = and bs
or :: [Bool] -> Bool
or [] = False
or (True:bs) = True
or (_:bs) = or bs
sum :: [Int] -> Int
sum [] = 0
sum (i:is) = i + sum is
length :: [a] -> Int
length [] = 0
length (_:as) = 1 + length as
add :: a -> [a] -> [a]
add a [] = [a]
add a (a':as) = a':add a as
(++) :: [a] -> [a] -> [a]
(++) [] as' = as'
(++) (a:as) as' = a : (as ++ as')
-- Higer order
map :: (a -> b) -> [a] -> [b]
map _ [] = []
map f (a:as) = f a : map f as
length0 :: [a] -> Int
length0 as = sum $ map giveOne as
doeElk :: [a] -> [(a -> b)] -> [b]
doeElk _ [] = []
doeElk as (f:fs)= map f as ++ (as `doeElk` fs)