From 8fad24c02c806c98d673868dc24670d8ddc0b0eb Mon Sep 17 00:00:00 2001 From: Michael Zhang Date: Thu, 28 Jan 2021 01:58:59 -0600 Subject: [PATCH] ch1 --- ch01.lhs | 38 ++++++++++++++++++++++++++++++++++++++ 1 file changed, 38 insertions(+) create mode 100644 ch01.lhs diff --git a/ch01.lhs b/ch01.lhs new file mode 100644 index 0000000..6c037e5 --- /dev/null +++ b/ch01.lhs @@ -0,0 +1,38 @@ +Chapter 1: Functional Programming +=== + +Exercise 1.2 +--- + +Trawling through Data.List we discovered the function + +> uncons :: [a] -> Maybe (a, [a]) + +of whose existence we were quite unconscious. Guess the definition of uncons. + +> uncons [] = Nothing +> uncons (hd : tl) = Just (hd, tl) + +Exercise 1.3 +--- + +The library Data.List does not provide functions + +> wrap :: a -> [a] +> unwrap :: [a] -> a +> single :: [a] -> Bool + +for wrapping a value into a singleton list, unwrapping a singleton list into +its sole occupant, and testing a list for being a singleton. This is a pity, +for the three functions can be very useful on occasions and will appear a +number of times in the rest of this book. Give appropriate definitions. + +> -- return should work here too +> wrap x = [x] +> +> -- TBH unwrap should be returning Maybe since otherwise this throws an +> -- exception on unmatched cases +> unwrap [x] = x +> +> single [x] = True +> single _ = False