Created
January 18, 2011 08:04
-
-
Save wavebeem/784115 to your computer and use it in GitHub Desktop.
list.hs
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module List where | |
data List a | |
= Null | |
| Node (List a) a | |
deriving (Show, Eq) | |
build = build' Null | |
where | |
build' Null [] = Null | |
build' list (x:xs) = Node (build' list xs) x | |
head Null = Nothing | |
head (Node list x) = Just x | |
tail Null = Nothing | |
tail (Node list x) = Just list | |
toe Null = Nothing | |
toe (Node Null x) = Just x | |
toe (Node list x) = toe list | |
display Null = "Null" | |
display (Node Null x) = show x | |
display (Node list x) = show x ++ " => " ++ display list | |
toList Null = [] | |
toList (Node list x) = x : toList list | |
append Null x = Node Null x | |
append (Node list y) x = Node (append list x) y | |
reverse Null = Null | |
reverse (Node list a) = (List.reverse list) `append` a | |
join sep Null = "" | |
join sep (Node Null x) = x | |
join sep (Node list x) = x ++ sep ++ join sep list | |
map f Null = Null | |
map f (Node list x) = Node (List.map f list) (f x) | |
main = | |
do | |
let xs = build [1 .. 9] | |
let sx = List.reverse xs | |
let aa = List.map show sx | |
let bb = join " --> " aa | |
putStrLn bb |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment