-
Notifications
You must be signed in to change notification settings - Fork 1
Expand file tree
/
Copy path12_map.hs
More file actions
executable file
·28 lines (22 loc) · 861 Bytes
/
12_map.hs
File metadata and controls
executable file
·28 lines (22 loc) · 861 Bytes
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
#!/usr/bin/env runhaskell
double :: Int -> Int
double a = 2 * a
triple :: Int -> Int
triple a = 3 * a
-- applyOverList is a function that receives a function, a list of ints, and
-- returns a list of the application of a function to each element.
applyOverList :: (Int -> Int) -> [Int] -> [Int]
applyOverList f xs = [ f x | x <- xs ]
-- The [ ... ] syntax is called list comprehension. It is only syntax sugar
-- that makes the code more mathematical and (surprisingly) more readable.
-- You can read it as: for each element of xs, apply f to it and return the
-- results as a list. Later we will see how to filter elements using the same
-- syntax.
doubleList = applyOverList double
tripleList = applyOverList triple
main :: IO ()
main = do
putStr "double: "
putStrLn (show (doubleList [2,3,4]))
putStr "triple: "
putStrLn (show (tripleList [2,3,4]))