Haskell & Functional Programming
Exercise 1
Write (two versions of) a function myReplicate that given an integer n and a value
v returns a list of length n initialized with v, namely all
elements are equal to v.
- Goal: Warming up!
- Expected output: Two implementations of the above function:
myReplicateRwhich uses recursion on natural numbers, andmyReplicateCthat uses combinators (i.e., functions likemap,filter,foldl/rfrom the Haskell Prelude).
Exercise 2
Write (two versions of) a function sumOdd that given a list of integers computes the
sum of the values that are odd.
Hint: consider the functions odd and even of the Prelude.
- Goal: Warming up (part 2)!
- Expected output: Two implementations of the above function:
sumOddRwhich uses recursion on lists, andsumOddCthat uses combinators (i.e., functions likemap,filter,foldl/rfrom the Haskell Prelude).
Exercise 3
Write (two versions of) a function repl that given a list xs and a integer n
returns a list containing the elements of xs replicated n times.
Hint: you can use the function myReplicate of Exercise 1.
- Goal: Playing with lists.
- Expected output: Two implementations of the above function:
replRwhich uses recursion on lists, andreplCthat uses combinators (i.e., functions likemap,filter,foldl/rfrom the Haskell Prelude).
Exercise 4
Write a function totalLength that given a list of strings xs
computes the sum of the lengths of the strings starting with the
character 'A'.
- Goal: Test your skills with lists and strings.
- Expected output: Two implementations of the above function:
totalLengthRwhich uses recursion on lists, andtotalLengthCthat uses combinators (i.e., functions likemap,filter,foldl/rfrom the Haskell Prelude).
Exercise 5
Write a function filterOdd that given a list xs returns a new
list obtained from xs by removing the elements at odd positions.
Hint: Here "odd positions" means the first, third, fifth, etc position.
- Goal: Playing with lists (part 2).
- Expected output: Two implementations of the above function:
filterOddRwhich uses recursion on lists, andfilterOddCthat uses combinators (i.e., functions likemap,filter,foldl/rfrom the Haskell Prelude).
Exercise 6
Write a function titlecase that given a string s converts it to
titlecase by uppercasing the first letter of every word.
Hint: consider using the function words, unwords of the
Prelude and the function toUpper of the module Data.Char. To
make accessible this last function in your code use import
Data.Char (toUpper).
- Goal: Experimenting with strings.
- Expected output: Two implementations of the above function:
titlecaseRwhich uses recursion on lists, andtitlecaseCthat uses combinators (i.e., functions likemap,filter,foldl/rfrom the Haskell Prelude).
Exercise 7
Write a function countVowelPali that given a list of strings xs
returns the total number of vowels in strings that are palindromes.
For example,
countVowelPali ["anna", "banana", "civic", "mouse"] = 4
- Goal: Fun with strings and lists (again :P).
- Expected output: Two implementations of the above function:
countVowelPaliwhich uses recursion on lists, andcountVowelPalithat uses combinators (i.e., functions likemap,filter,foldl/rfrom the Haskell Prelude).
Exercise 8
Recall the higher-order combinator map from the Prelude.
Implement it using the combinator foldl.
- Goal: Experimenting with combinators.
- Expected output: The required implementation of the
mapcombinator.
Exercise 9
Consider the following definition of binary trees:
data IntTree = Leaf Int | Node (Int, IntTree, IntTree)
- Implement
tmap, a "tree version" of themapcombinator. More precisely, the functiontmapshould take a functionfand a treetand should applyfto each value int. - Using
tmapimplement the functionsuccTreetaking a treetand computing a tree whose elements are the successors of the values int. - Write a function
sumSucctaking a treetand computing the sum of the elements ofsuccTree t.
- Goal: Experimenting with trees.
- Expected output: An implementation of the three required functions.