-
Notifications
You must be signed in to change notification settings - Fork 0
/
14higherOrderFunctions.hs
120 lines (68 loc) · 3.04 KB
/
14higherOrderFunctions.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
103
104
105
106
107
108
109
110
111
112
113
114
115
myMap _ [] = []
myMap function list = function (head list) : myMap function (tail list)
--define myMap2 ( function, array ) = return ( function( head array ) : myMap2 ( function, array[1::])
myFiler _ [] = []
myFiler criteria list = if criteria (head list) then (head list ) : myFiler criteria (tail list)
else myFiler criteria (tail list)
myFiler2 _ [] = []
myFiler2 criteria list = if criteria (head list ) then (head list) : restOfTheCriteriadList
else restOfTheCriteriadList
where restOfTheCriteriadList = myFiler2 criteria (tail list)
-- implement remove:
myremove _ [] = []
myremove criteria list = if criteria (head list) then restOfTheCriteriadList
else (head list) : restOfTheCriteriadList
where restOfTheCriteriadList = myremove criteria (tail list)
-- implement reduce/foldl
reduce _ inital [] = inital
reduce operator inital list = reduce operator (operator inital (head list)) (tail list)
-- implement foldr
reduce2 operator initial [] = initial
reduce2 operator inital list = operator (head list) ( reduce2 operator inital (tail list))
{--
reduce + 0 [1,2,3,4,5]
reduce + 1 [2,3,4,5]
reduce + 3 [3,4,5]
.
.
reduce + 15 []
====================
(reduce + 0 [1,2,3,4,5])
1 + (reduce + 0 [2,3,4,5])
1 + 2 + (reduce + 0 [3,4,5])
1 + 2 + 3 + (reduce + 0 [4,5])
1 + 2 + 3 + 4 + ( reduce + 0 [5] )
1 + 2 + 3 + 4 + 5 + (reduce + 0 [] )
1 + 2 + 3 + 4 + 5 + 0
--}
-- implementing reduce_left and reduce_right again for practice:
-- tail recursion (no stack created)
reduce_left op init [] = init
reduce_left op init (x:xs) = reduce_left op (op init x) xs
-- right_reduce, stack created
reduce_right op init [] = init
reduce_right op init (x:xs) = op x (reduce_right op init xs)
-- concat all strings of a list
-- [ "Sdadsa", "sadasd", "Sadasdas", "adsda"]
concatAll [] = []
concatAll list = foldl (++) "" list
-- create sumOfSquares, which squares every value in a list and then takes the sum of it:
sumOfSquares list = foldl (+) 0 (map (^2) list)
-- use foldl to reverse a list
rcons x y = y:x
myreverse2 list = foldl rcons [] list
-- create function elem
myElem element list = if length filteredList >= 1 then True
else False
where filteredList = filter (== element) list
-- alternate:
myElem2 element list = foldl (||) False list3
where list3 = (map (== element) list)
{-- In mathematics,
the harmonic series is the sum of 1/1 + 1/2 + 1/3 + 1/4 ....... + 1 / n
Write a function harmonic that takes an argument n
and calculates the sum of the series to n. Make sure to use lazy evaluation. --}
harmonicSeries n = foldr (+) 0 harmonicList
where harmonicList = map (\x -> (1/x)) [1..n]
-- ^ todo lazily
-- https://stackoverflow.com/questions/61473630/compute-harmonic-function-lazily