-
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
Showing
9 changed files
with
123 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 |
---|---|---|
@@ -0,0 +1,9 @@ | ||
module Problem1 where | ||
|
||
-- Problem 1 | ||
-- Find the last element of a list. | ||
|
||
myLast :: [a] -> a | ||
myLast [x] = x | ||
myLast (_ : xs) = myLast xs | ||
myLast _ = error "The list should be nonempty." |
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,9 @@ | ||
module Problem2 where | ||
|
||
-- Problem 2 | ||
-- Find the last but one element of a list. | ||
|
||
myButLast :: [a] -> a | ||
myButLast [x, _] = x | ||
myButLast (_ : xs) = myButLast xs | ||
myButLast _ = error "There should be at least 2 items in the list." |
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,9 @@ | ||
module Problem3 where | ||
|
||
-- Problem 3 | ||
-- Find the K'th element of a list. The first element in the list is number 1. | ||
|
||
elementAt :: [a] -> Int -> a | ||
elementAt [] _ = error "Position does not exist." | ||
elementAt (x : _) 1 = x | ||
elementAt (_ : xs) n = elementAt xs (n - 1) |
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,14 @@ | ||
module Problem4 where | ||
|
||
-- Problem 4 | ||
-- Find the number of elements of a list. | ||
|
||
myLength :: [a] -> Int | ||
myLength [] = 0 | ||
myLength (x : xs) = 1 + myLength xs | ||
|
||
|
||
-- Better, using foldr. | ||
|
||
myLength2 :: [a] -> Int | ||
myLength2 = foldr (\x y -> 1 + y) 0 |
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,26 @@ | ||
module Problem5 where | ||
|
||
import DList | ||
|
||
-- Problem 5 | ||
-- Reverse a list. | ||
|
||
|
||
-- This solution uses an explicit accumulation parameter | ||
-- and runs in linear time. | ||
|
||
myReverse :: [a] -> [a] | ||
myReverse xs = snd $ auxReverse (xs, []) | ||
where auxReverse ([], bs) = ([], bs) | ||
auxReverse ( a : as, bs) = auxReverse (as, a : bs) | ||
|
||
|
||
-- This is essentially the same algorithm but hides | ||
-- the acummulation parameter in th Dlist module. | ||
-- It works slightly slower than myReverse but still | ||
-- in linear time. | ||
|
||
myReverse2 :: [a] -> [a] | ||
myReverse2 = toList . reversoToDList | ||
where reversoToDList [] = nilD | ||
reversoToDList (x : xs) = reversoToDList xs +++ singleton x |
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,9 @@ | ||
module Problem6 where | ||
|
||
import Problem5 (myReverse) | ||
|
||
-- Problem 6 | ||
-- Find out whether a list is a palindrome. | ||
|
||
isPalindrome :: Eq a => [a] -> Bool | ||
isPalindrome xs = xs == myReverse xs |
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 @@ | ||
module Problem7 where | ||
|
||
-- Problem 7 | ||
-- Flatten a nested list structure. | ||
|
||
data NestedList a = Elem a | List [NestedList a] | ||
|
||
flatten :: NestedList a -> [a] | ||
flatten (Elem x) = [x] | ||
flatten (List ls) = concatMap flatten ls |
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,11 @@ | ||
module Problem8 where | ||
|
||
-- Problem 8 | ||
-- Eliminate consecutive duplicates of list elements. | ||
|
||
compress :: Eq a => [a] -> [a] | ||
compress [] = [] | ||
compress [x] = [x] | ||
compress (x1 : x2 : xs) = | ||
if x1 == x2 then rest else x1 : rest where | ||
rest = compress (x2 : xs) |
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,26 @@ | ||
module Problem9 where | ||
|
||
import Data.List (group) | ||
|
||
-- Problem 9 | ||
-- Pack consecutive duplicates of list elements into sublists. | ||
|
||
pack :: Eq a => [a] -> [[a]] | ||
pack [] = [] | ||
pack [x] = [[x]] | ||
pack (x1 : x2 : xs) | ||
| x1 == x2 = (x1 : head rest) : tail rest | ||
| otherwise = [x1] : rest | ||
where rest = pack (x2 : xs) | ||
|
||
|
||
-- Using span. | ||
pack2 :: Eq a => [a] -> [[a]] | ||
pack2 [] = [] | ||
pack2 (x : xs) = l : pack2 r | ||
where (l, r) = span (==x) (x : xs) | ||
|
||
-- One can also use the built in group function. | ||
|
||
pack3 :: Eq a => [a] -> [[a]] | ||
pack3 = group |