Skip to content

Instantly share code, notes, and snippets.

@jspahrsummers
Created April 3, 2011 07:28
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save jspahrsummers/900266 to your computer and use it in GitHub Desktop.
Save jspahrsummers/900266 to your computer and use it in GitHub Desktop.
data List a =
Nil |
Cons a (List a)
listLength :: Num b => List a -> b
listLength Nil = 0
listLength (Cons x xs) = 1 + listLength xs
listHead :: List a -> a
listHead (Cons x xs) = x
listTail :: List a -> List a
listTail (Cons x xs) = xs
listFoldl :: (a -> b -> a) -> a -> List b -> a
listFoldl f a Nil = a
-- my version
listFoldl f a (Cons x xs) = f (listFoldl f a xs) x
-- from the tutorial solution at http://en.wikibooks.org/wiki/Haskell/YAHT/Type_basics/Solutions#Recursive_Datatypes
--listFoldl f y (Cons x xs) = listFoldl f (f y x) xs
listFoldr :: (a -> b -> b) -> b -> List a -> b
listFoldr f a Nil = a
listFoldr f a (Cons x xs) = f x (listFoldr f a xs)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment