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:
myReplicateR
which uses recursion on natural numbers, andmyReplicateC
that uses combinators (i.e., functions likemap
,filter
,foldl/r
from 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:
sumOddR
which uses recursion on lists, andsumOddC
that uses combinators (i.e., functions likemap
,filter
,foldl/r
from 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:
replR
which uses recursion on lists, andreplC
that uses combinators (i.e., functions likemap
,filter
,foldl/r
from 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:
totalLengthR
which uses recursion on lists, andtotalLengthC
that uses combinators (i.e., functions likemap
,filter
,foldl/r
from 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:
filterOddR
which uses recursion on lists, andfilterOddC
that uses combinators (i.e., functions likemap
,filter
,foldl/r
from 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:
titlecaseR
which uses recursion on lists, andtitlecaseC
that uses combinators (i.e., functions likemap
,filter
,foldl/r
from 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:
countVowelPali
which uses recursion on lists, andcountVowelPali
that uses combinators (i.e., functions likemap
,filter
,foldl/r
from 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
map
combinator.
Exercise 9
Consider the following definition of binary trees:
data IntTree = Leaf Int | Node (Int, IntTree, IntTree)
- Implement
tmap
, a "tree version" of themap
combinator. More precisely, the functiontmap
should take a functionf
and a treet
and should applyf
to each value int
. - Using
tmap
implement the functionsuccTree
taking a treet
and computing a tree whose elements are the successors of the values int
. - Write a function
sumSucc
taking a treet
and computing the sum of the elements ofsuccTree t
.
- Goal: Experimenting with trees.
- Expected output: An implementation of the three required functions.