Skip to content

Commit

Permalink
File names changed.
Browse files Browse the repository at this point in the history
  • Loading branch information
sonatsuer committed Sep 29, 2015
1 parent 5b7a22c commit ae3001b
Show file tree
Hide file tree
Showing 9 changed files with 123 additions and 0 deletions.
9 changes: 9 additions & 0 deletions Problem01.hs
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."
9 changes: 9 additions & 0 deletions Problem02.hs
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."
9 changes: 9 additions & 0 deletions Problem03.hs
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)
14 changes: 14 additions & 0 deletions Problem04.hs
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
26 changes: 26 additions & 0 deletions Problem05.hs
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
9 changes: 9 additions & 0 deletions Problem06.hs
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
10 changes: 10 additions & 0 deletions Problem07.hs
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
11 changes: 11 additions & 0 deletions Problem08.hs
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)
26 changes: 26 additions & 0 deletions Problem09.hs
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

0 comments on commit ae3001b

Please sign in to comment.