aboutsummaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorJan Sucan <jan@jansucan.com>2023-03-21 21:16:01 +0100
committerJan Sucan <jan@jansucan.com>2023-03-21 21:16:01 +0100
commitca95e3929826664716cecc73b4efd0c5ce766ccf (patch)
treef8379603edc52ae2bfc6bb6749e8e00473ed41de
parent0d0c3870d945f07e93337c2c2941d85106f807d6 (diff)
4_b_5 and 4_b_6: Add solutions
-rw-r--r--README.md4
-rw-r--r--ch04/4_b_6.hs30
2 files changed, 32 insertions, 2 deletions
diff --git a/README.md b/README.md
index fee3989..2caaa7f 100644
--- a/README.md
+++ b/README.md
@@ -76,8 +76,8 @@ more visible in the list the first exercise of a group is in bold italics.
| 4_b_2 | yes | 98 | |
| 4_b_3 | yes | | |
| 4_b_4 | yes | | |
-| 4_b_5 | | | |
-| 4_b_6 | | | |
+| 4_b_5 | yes, in 4_b_6 | | |
+| 4_b_6 | yes | | |
| 4_b_7 | | | |
| 4_b_8 | | | |
| 4_b_9 | | | |
diff --git a/ch04/4_b_6.hs b/ch04/4_b_6.hs
new file mode 100644
index 0000000..91604d4
--- /dev/null
+++ b/ch04/4_b_6.hs
@@ -0,0 +1,30 @@
+-- 1. The Prelude function concat concatenates a list of lists into a single list
+-- and has the following type:
+--
+-- -- file: ch04/ch04.exercises.hs
+-- concat :: [[a]] -> [a]
+--
+-- 2. Write your own definition of concat using foldr.
+
+myConcat :: [[a]] -> [a]
+myConcat xs = foldr (++) [] xs
+
+-- ghci> :l 4_b_6.hs
+-- [1 of 1] Compiling Main ( 4_b_6.hs, interpreted )
+-- Ok, one module loaded.
+-- ghci> concat []
+-- []
+-- ghci> concat [[]]
+-- []
+-- ghci> concat [[1]]
+-- [1]
+-- ghci> concat [[1], [2,3], [4,5,6]]
+-- [1,2,3,4,5,6]
+-- ghci> myConcat []
+-- []
+-- ghci> myConcat [[]]
+-- []
+-- ghci> myConcat [[1]]
+-- [1]
+-- ghci> myConcat [[1], [2,3], [4,5,6]]
+-- [1,2,3,4,5,6]