-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathch04_TFwH_Bird_4F.hs
37 lines (26 loc) · 893 Bytes
/
ch04_TFwH_Bird_4F.hs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
-- Chapter 4 Exercise F: "Dual" / inverse view of List
-- Thinking Functionally with Haskell (Richard Bird)
-- Vince Reuter
-- June 2019
import Test.QuickCheck
data List a = Nil | Snoc (List a) a deriving (Show, Eq, Ord)
instance Functor List where
fmap _ Nil = Nil
fmap f (Snoc xs x) = Snoc (fmap f xs) (f x)
head' :: List a -> a
head' Nil = error "empty list"
head' (Snoc Nil x) = x
head' (Snoc xs x) = head' xs
tail' :: List a -> [a]
tail' = tail . fromList
toList :: [a] -> List a
toList = foldl (\acc x -> Snoc acc x) Nil
fromList :: (List a) -> [a]
fromList xs = go xs []
where go Nil acc = acc
go (Snoc ys y) acc = go ys (y:acc)
prop_UniversalListRoundtrip xs = (fromList . toList) xs == xs
where types = (xs::[Int])
prop_NonemptyHeadTail xs =
let l = toList xs in (not (null xs) ==> head' l : tail' l == xs)
where types = (xs::[Char])