diff options
| author | Jan Sucan <jan@jansucan.com> | 2023-03-11 17:50:29 +0100 |
|---|---|---|
| committer | Jan Sucan <jan@jansucan.com> | 2023-03-11 17:50:29 +0100 |
| commit | f9d8688be8180eb572ec8b4f8696d3c15064bd07 (patch) | |
| tree | 8cda32310bafbece2605ea8f63ee5667609901cb | |
| parent | 010404abf1321443677efc91d21eb7f93c91a324 (diff) | |
3_b_7: Add solution
| -rw-r--r-- | README.md | 2 | ||||
| -rw-r--r-- | ch03/3_b_7.hs | 32 |
2 files changed, 33 insertions, 1 deletions
@@ -59,7 +59,7 @@ more visible in the list the first exercise of a group is in bold italics. | 3_b_4 | yes | | | | 3_b_5 | yes | | | | 3_b_6 | yes | 70 | | -| 3_b_7 | | | | +| 3_b_7 | yes | | | | 3_b_8 | | | | | 3_b_9 | | | | | 3_b_10 | | | | diff --git a/ch03/3_b_7.hs b/ch03/3_b_7.hs new file mode 100644 index 0000000..51f7849 --- /dev/null +++ b/ch03/3_b_7.hs @@ -0,0 +1,32 @@ +-- Define a function that joins a list of lists together using a separator value. +-- +-- -- file: ch03/Intersperse.hs +-- intersperse :: a -> [[a]] -> [a] +-- +-- The separator should appear between elements of the list, but should not +-- follow the last element. Your function should behave as follows. +-- +-- ghci> :load Intersperse +-- [1 of 1] Compiling Main ( Intersperse.hs, interpreted ) +-- Ok, modules loaded: Main. +-- ghci> intersperse ',' [] +-- "" +-- ghci> intersperse ',' ["foo"] +-- "foo" +-- ghci> intersperse ',' ["foo","bar","baz","quux"] +-- "foo,bar,baz,quux" + +myIntersperse :: a -> [[a]] -> [a] +myIntersperse _ [] = [] +myIntersperse _ (x:[]) = x +myIntersperse s (x:xs) = x ++ s:(myIntersperse s xs) + +-- ghci> :l 3_b_7.hs +-- [1 of 1] Compiling Main ( 3_b_7.hs, interpreted ) +-- Ok, one module loaded. +-- ghci> myIntersperse ',' [] +-- "" +-- ghci> myIntersperse ',' ["foo"] +-- "foo" +-- ghci> myIntersperse ',' ["foo","bar","baz","quux"] +-- "foo,bar,baz,quux" |
