Skip to content

Commit

Permalink
5, 6, 7. We rock.
Browse files Browse the repository at this point in the history
  • Loading branch information
Thomasvdam committed Jun 11, 2014
1 parent 0fc3199 commit 3339a9e
Show file tree
Hide file tree
Showing 4 changed files with 31 additions and 0 deletions.
6 changes: 6 additions & 0 deletions 4.hs
Original file line number Diff line number Diff line change
@@ -1 +1,7 @@
-- Find the number of elements of a list.

myLength :: [a] -> Int
myLength [] = 0
myLength (x:xs) = 1 + myLength xs

main = print (myLength [1..100])
8 changes: 8 additions & 0 deletions 5.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,8 @@
-- Reverse a list.

myReverse :: [a] -> [a]
myReverse [] = []
myReverse [x] = [x]
myReverse (x:xs) = (myReverse xs) ++ [x]

main = print (myReverse [1..10])
7 changes: 7 additions & 0 deletions 6.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
-- Find out whether a list is a palindrome. A palindrome can be read forward
-- or backward; e.g. (x a m a x).

myPalindrome :: Eq a => [a] -> Bool
myPalindrome xs = xs == reverse xs

main = print (myPalindrome "racecar")
10 changes: 10 additions & 0 deletions 7.hs
Original file line number Diff line number Diff line change
@@ -0,0 +1,10 @@
-- Flatten a nested list structure.

data NestedList a = Elem a | List [NestedList a]

myFlatten :: NestedList a -> [a]
myFlatten (Elem x) = [x]
myFlatten (List []) = []
myFlatten (List (x:xs)) = myFlatten x ++ myFlatten (List xs)

main = print (myFlatten (List [Elem 1, List [Elem 2, List [Elem 3, Elem 4], Elem 5]]))

0 comments on commit 3339a9e

Please sign in to comment.