-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathAppend.hs
More file actions
53 lines (41 loc) · 990 Bytes
/
Copy pathAppend.hs
File metadata and controls
53 lines (41 loc) · 990 Bytes
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
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
module DList
(
DList
, fromList
, toList
, empty
, append
, cons
, dfoldr
) where
import Data.Monoid
newtype DList a = DL {
unDL :: [a] -> [a]
}
append :: DList a -> DList a -> DList a
append xs ys = DL (unDL xs . unDL ys)
append' :: DList a -> DList a -> DList a
append' (DL xs) (DL ys) = DL (xs . ys)
fromList :: [a] -> DList a
fromList xs = DL (xs ++)
toList :: DList a -> [a]
toList (DL xs) = xs []
empty :: DList a
empty = DL id
cons :: a -> DList a -> DList a
cons x (DL xs) = DL ((x:) . xs)
infixr `cons`
dfoldr :: (a -> b -> b) -> b -> DList a -> b
dfoldr f x xs = foldr f x (toList xs)
safeHead :: DList a -> Maybe a
safeHead xs = case toList xs of
(y:_) -> Just y
_ -> Nothing
dmap :: (a -> b) -> DList a -> DList b
dmap f = dfoldr go empty
where go x xs = cons (f x) xs
instance Functor DList where
fmap = dmap
instance Monoid (DList a) where
mempty = empty
mappend = append