-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
0fc3199
commit 3339a9e
Showing
4 changed files
with
31 additions
and
0 deletions.
There are no files selected for viewing
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
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]) |
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
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]) |
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
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") |
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
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]])) |