ch1
This commit is contained in:
commit
8fad24c02c
1 changed files with 38 additions and 0 deletions
38
ch01.lhs
Normal file
38
ch01.lhs
Normal file
|
@ -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
|
Loading…
Reference in a new issue