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 --- README.md | 2 +- ch06/6_a_1.txt | 22 ++++++++++++++++++++++ 2 files changed, 23 insertions(+), 1 deletion(-) create mode 100644 ch06/6_a_1.txt diff --git a/README.md b/README.md index 91cee4c..57f55d0 100644 --- a/README.md +++ b/README.md @@ -104,7 +104,7 @@ of a group is in bold italics. | 4_b_10 | yes | | | | **_5_a_1_** | yes | 130 | 5. Writing a library: working with JSON data | | 5_a_2 | yes, in 5_a_1 | | | -| **_6_a_1_** | | 162 | 6. Using typeclasses | +| **_6_a_1_** | yes | 162 | 6. Using typeclasses | | 6_a_2 | | | | | **_8_a_1_** | | 205 | 8. Efficient file processing, regular expressions, and file name matching | | 8_a_2 | | | | 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