Skip to content

Commit

Permalink
added myListFunctions
Browse files Browse the repository at this point in the history
  • Loading branch information
amidvidy committed Aug 15, 2012
1 parent b913174 commit 74a801f
Showing 1 changed file with 34 additions and 0 deletions.
34 changes: 34 additions & 0 deletions real-world-haskell/ch4/myListFunctions.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,34 @@
-- same as 'length'
myLength :: [a] -> Int
myLength (x:xs) = 1 + myLength xs
myLength _ = 0

-- same as 'null'
myNull :: [a] -> Bool
myNull [] = True
myNull _ = False

-- same as 'head'
myHead :: [a] -> a
myHead (x:xs) = x
myHead _ = error "myHead takes a non-empty list"

mySafeHead :: [a] -> Maybe a
mySafeHead (x:xs) = Just x
mySafeHead _ = Nothing

-- same as 'tail'
myTail :: [a] -> [a]
myTail (x:xs) = xs
myTail _ = error "myTail takes a non-empty list"

-- same as 'last'
myLast :: [a] -> a
myLast [x] = x
myLast (x:xs) = myLast xs

-- same as 'init'
myInit :: [a] -> [a]
myInit [x] = []
myInit (x:xs) = x : myInit xs

0 comments on commit 74a801f

Please sign in to comment.