31 lines
699 B
Haskell
31 lines
699 B
Haskell
|
module StrictList (
|
||
|
StrictList(..)
|
||
|
, foldl
|
||
|
, reverse
|
||
|
, head
|
||
|
, tail
|
||
|
) where
|
||
|
|
||
|
import Prelude hiding (foldl, head, reverse, tail)
|
||
|
|
||
|
data StrictList a = !a :$ !(StrictList a) | Empty
|
||
|
deriving (Show, Eq)
|
||
|
|
||
|
infixr 5 :$
|
||
|
|
||
|
foldl :: (b -> a -> b) -> b -> StrictList a -> b
|
||
|
foldl f = lgo
|
||
|
where lgo z Empty = z
|
||
|
lgo z (x :$ xs) = let z' = f z x in z' `seq` lgo z' xs
|
||
|
|
||
|
reverse :: StrictList a -> StrictList a
|
||
|
reverse = foldl (flip (:$)) Empty
|
||
|
|
||
|
head :: StrictList a -> a
|
||
|
head (x :$ _ ) = x
|
||
|
head Empty = error "Empty StrictList has no head"
|
||
|
|
||
|
tail :: StrictList a -> StrictList a
|
||
|
tail (_ :$ xs) = xs
|
||
|
tail Empty = error "Empty StrictList has no tail"
|