From a4d44a9b0867b9f7b0ce97d4c2d9078a726885a5 Mon Sep 17 00:00:00 2001 From: Jan Sucan Date: Sun, 16 Apr 2023 08:13:55 +0200 Subject: 6_a_1: Add solution --- ch06/6_a_1.txt | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) create mode 100644 ch06/6_a_1.txt (limited to 'ch06') diff --git a/ch06/6_a_1.txt b/ch06/6_a_1.txt new file mode 100644 index 0000000..0f5e23d --- /dev/null +++ b/ch06/6_a_1.txt @@ -0,0 +1,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)] -- cgit v1.2.3