From ae3001b2ef74792e2565625c892efc56df5dfeeb Mon Sep 17 00:00:00 2001 From: sonatsuer Date: Tue, 29 Sep 2015 16:51:45 +0300 Subject: [PATCH] File names changed. --- Problem01.hs | 9 +++++++++ Problem02.hs | 9 +++++++++ Problem03.hs | 9 +++++++++ Problem04.hs | 14 ++++++++++++++ Problem05.hs | 26 ++++++++++++++++++++++++++ Problem06.hs | 9 +++++++++ Problem07.hs | 10 ++++++++++ Problem08.hs | 11 +++++++++++ Problem09.hs | 26 ++++++++++++++++++++++++++ 9 files changed, 123 insertions(+) create mode 100644 Problem01.hs create mode 100644 Problem02.hs create mode 100644 Problem03.hs create mode 100644 Problem04.hs create mode 100644 Problem05.hs create mode 100644 Problem06.hs create mode 100644 Problem07.hs create mode 100644 Problem08.hs create mode 100644 Problem09.hs diff --git a/Problem01.hs b/Problem01.hs new file mode 100644 index 0000000..4ddee01 --- /dev/null +++ b/Problem01.hs @@ -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." diff --git a/Problem02.hs b/Problem02.hs new file mode 100644 index 0000000..c107b14 --- /dev/null +++ b/Problem02.hs @@ -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." diff --git a/Problem03.hs b/Problem03.hs new file mode 100644 index 0000000..00eeb37 --- /dev/null +++ b/Problem03.hs @@ -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) diff --git a/Problem04.hs b/Problem04.hs new file mode 100644 index 0000000..bae0f0d --- /dev/null +++ b/Problem04.hs @@ -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 diff --git a/Problem05.hs b/Problem05.hs new file mode 100644 index 0000000..1bfea15 --- /dev/null +++ b/Problem05.hs @@ -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 diff --git a/Problem06.hs b/Problem06.hs new file mode 100644 index 0000000..f05a36b --- /dev/null +++ b/Problem06.hs @@ -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 diff --git a/Problem07.hs b/Problem07.hs new file mode 100644 index 0000000..7964007 --- /dev/null +++ b/Problem07.hs @@ -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 diff --git a/Problem08.hs b/Problem08.hs new file mode 100644 index 0000000..0d4c604 --- /dev/null +++ b/Problem08.hs @@ -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) diff --git a/Problem09.hs b/Problem09.hs new file mode 100644 index 0000000..3fb9ecf --- /dev/null +++ b/Problem09.hs @@ -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