99-problems/Haskell.org
2023-11-17 15:21:13 +07:00

3.7 KiB

99 Haskell Problems

https://wiki.haskell.org/H-99:_Ninety-Nine_Haskell_Problems

#1 Find the last element of a list.

λ> myLast [1,2,3,4]
4
λ> myLast ['x','y','z']
'z'
Solution
myLast [] = error "Cannot accept an empty list"
myLast [x] = x
myLast (_:xs) = myLast xs

myLast [1..5]

#2 Find the last but one element of a list.

λ> myButLast [1,2,3,4]
3
λ> myButLast ['a'..'z']
'y'
Solution
myButLast x
  | length x < 2 = error "Cannot accept an empty list"
  | otherwise = case x of
                  [x,y] -> x
                  (x:xs) -> myButLast xs

#3 Find the K'th element of a list.

The first element in the list is number 1.

λ> elementAt [1,2,3] 2
2
λ> elementAt "haskell" 5
'e'
Solution
elementAt (x:xs) i = if i == 1 then x else elementAt xs (i - 1)

#4 Find the number of elements of a list.

λ> myLength [123, 456, 789]
3
λ> myLength "Hello, world!"
13
Solution
myLength [] = 0
myLength xs = foldl (\acc _ -> acc + 1) 0 xs
-- or
myLength' [] = 0
myLength' [x] = 1
myLength' (_:xs) = 1 + myLength xs

#5 Reverse a list.

λ> myReverse "A man, a plan, a canal, panama!"
"!amanap ,lanac a ,nalp a ,nam A"
λ> myReverse [1,2,3,4]
[4,3,2,1]
Solution
myReverse = go []
  where go acc [] = acc
        go acc (x:xs) = go (x:acc) xs

#6 Find out whether a list is a palindrome

λ> isPalindrome [1,2,3]
False
λ> isPalindrome "madamimadam"
True
λ> isPalindrome [1,2,4,8,16,8,4,2,1]
True
Solution
isPalindrome xs = xs == reverse xs

#7 Flatten a nested list structure.

Transform a list, possibly holding lists as elements into a `flat' list by replacing each list with its elements (recursively).

Example:

,* (my-flatten '(a (b (c d) e)))
(A B C D E)

Example in Haskell:

We have to define a new data type, because lists in Haskell are homogeneous.

data NestedList a = Elem a | List [NestedList a]
λ> flatten (Elem 5)
[5]
λ> flatten (List [Elem 1, List [Elem 2, List [Elem 3, Elem 4], Elem 5]])
[1,2,3,4,5]
λ> flatten (List [])
[]
Solution
flatten = reverse . go []
  where go acc (List []) = acc
        go acc (Elem x) = x:acc
        go acc (List (x:xs)) = go (go acc x) (List xs)

#8 Eliminate consecutive duplicates of list elements

If a list contains repeated elements they should be replaced with a single copy of the element. The order of the elements should not be changed.

Example:

,* (compress '(a a a a b c c a a d e e e e))
(A B C A D E)

Example in Haskell:

λ> compress "aaaabccaadeeee"
"abcade"
Solution

This is not great…

compress xs = reverse $ go [] xs
  where go acc [] = acc
        go [] (x:xs) = go [x] xs
        go [a] [x]
            | a == x = [a]
            | otherwise = [x,a]
        go (a:as) [x]
            | a == x = a:as
            | otherwise = x:a:as
        go [a] (x:xs)
            | a == x = go [a] xs
            | otherwise = go [x,a] xs
        go (a:as) (x:xs)
            | a == x = go (a:as) xs
            | otherwise = go (x:a:as) xs

Especially when one of the solutions is the following;

import Data.List

compress :: Eq a => [a] -> [a]
compress = map head . group