aboutsummaryrefslogtreecommitdiff
path: root/ch06/6_a_1.txt
blob: 0f5e23dc4930f84a9f06a316da55041e137403d6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
-- Load the Control.Arrow module into ghci, and find out what the second
-- function does.

-- The example immediately preceding the exercise (instance of the JSON
-- typeclass for the JObj type) is a big hint about what the 'second' function
-- does.
--
-- It turns a function F accepting a value of type B and returning a value of
-- type C into a function accepting a 2-tuple and returning a 2-tuple. The
-- returned function applies F to the second element of the input 2-tuple (which
-- is of type B) and keeps the first element of the 2-tuple unchanged.

ghci> import Control.Arrow

ghci> :t second
second :: Arrow a => a b c -> a (d, b) (d, c)

ghci> map (second odd) [(1, 1), (2, 2), (3, 3), (4, 4)]
[(1,True),(2,False),(3,True),(4,False)]

ghci> map (second (\x -> 2 * x)) [(1, 1), (2, 2), (3, 3), (4, 4)]
[(1,2),(2,4),(3,6),(4,8)]