-
Notifications
You must be signed in to change notification settings - Fork 12
/
E05_RecursionSchemes.hs
71 lines (57 loc) · 1.96 KB
/
E05_RecursionSchemes.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
module E05_RecursionSchemes where
import Common
{-
This is what we all came here for. Finally we'll start using higher order functions!
First a few examples:
-}
celebrities =
["conan o'brien", "steve wozniak",
"Beyoncé", "Queen Latifah",
"simon peyton jones", "Taylor Swift"]
nameLengths = map length celebrities
-- [13,13,7,13,18,12]
reversedNames = map reverse celebrities
-- ["neirb'o nanoc","kainzow evets","écnoyeB","hafitaL neeuQ","senoj notyep nomis","tfiwS rolyaT"]
isSimon = filter (== "simon peyton jones") celebrities
-- ["simon peyton jones"]
startsWithS = filter (\celeb -> head celeb == 's' ) celebrities
-- ["steve wozniak","simon peyton jones"]
{-
Whoa, new stuff! There's a lambda function in there:
(\celeb -> head celeb == 's' )
Here we bind the first (and only) argument to the name celeb, then
in the function body we take the head of celeb and checks if the character is an 's'
Let's look at another lambda function:
-}
startsWithT = filter (\(x:xs) -> x == 'T') celebrities
-- ["Taylor Swift"]
{-
Would you look at that! We can even pattern match *inside* lambdas.
-}
{-
Exercise:
Define the add1 function so it adds 1 to all the numbers in a list
-}
add1 :: [Int] -> [Int]
add1 list = _YOUR_CODE_HERE
{-
Exercise:
Define the numsAsStrings function so it converts the integers in a list to strings.
tip: the `show` function converts ints to strings
-}
numsAsStrings :: [Int] -> [String]
numsAsStrings list = _YOUR_CODE_HERE
{-
Exercise:
Define the greaterThan2 function so it returns the integers from a list
that are greater than 2
-}
greaterThan2 :: [Int] -> [Int]
greaterThan2 list = _YOUR_CODE_HERE
{-
Exercise:
Define the filterNot function that works just like filter but inverts the condition.
In other words, filterNot will *keep* the exact elements that filter would *remove*
-}
filterNot :: (Int -> Bool) -> [Int] -> [Int]
filterNot condition list = _YOUR_CODE_HERE