Prab's Code Blog

Because talk is cheap, show me the code!

Functional Programming Series - Part 1

I have been intrigued by functional programming for past couple of years, but never quite prioritized it bigtime. Recently I came across this amazing talk which inspired me to go all-in to learn FP.

I chose Haskell as my preferred language and started with the book The Haskell Road to Logic, Maths and Programming. The reason was primarily because this book induces functional thinking without being too theoretical.

I started solving exercises from the book. This post and a few subsequent ones will document my solutions to some of the exercise problems. These solutions may not be most concise or efficient because I am just beginning to learn Haskell.

Define a method removeFst which removes the first instance of an element from a list

removeFst.hs
1
2
3
4
removeFst :: (Eq a) => [a] -> a -> [a]
removeFst (x:xs) y = if x == y
                      then xs
                        else x : removeFst xs y

Define a function which counts number of instances of an element in a list

countChars.hs
1
2
3
4
countChars :: Char -> String -> Int
countChars y [] = 0
countChars y (x:xs) | y == x = 1 + countChars y xs
                    | otherwise = countChars y xs

Define a function named blowUp that takes an string and creates a new string from it by repeating ith character in i times. For example: abcdef should return abbcccddddeeeeeffffff

blowUp.hs
1
2
3
blowUp :: Int -> String -> String
blowUp n [x] = take n [x,x..]
blowUp n (x:xs) = (blowUp n [x]) ++ (blowUp (n+1) xs)

Write a function to sort a list of orderable elements

This is a translation of the famous quick sort algorithm to Haskell.

sort.hs
1
2
3
4
5
6
sort :: (Ord a) => [a] -> [a]
sort [] = []
sort [x] = [x]
sort (x:xs) = sort greater ++ [x] ++ sort lesser
                where greater = [y | y <- xs, y > x]
                      lesser = [y | y <- xs, y < x ]

Write a function that takes a big string and a small string and tells whether the big string starts with the small string

startWith.hs
1
2
3
startWith :: String -> String -> Bool
startWith (x:xs) [y] = y == x
startWith (x:xs) (y:ys) = y == x && startWith xs ys

Write a function that takes a big string and a small string and tells whether the big string contains the small string as a substring

I use the previously defined startWith function to solve this.

substring.hs
1
2
3
contains :: String -> String -> Bool
contains (x:xs) [] = True
contains (x:xs) y = startWith (x:xs) y || startWith xs y

Comments