Prelude.sum
[*]Prelude.last
[*]Prelude.init
[*]Prelude.minimum
[*]Prelude.concat
[*](Prelude.++)
[*]Prelude.zip
[*]Data.List.isPrefixOf
[*]Prelude.elem
[*]Data.List.nub
[*]Data.List.tails
[*]Data.List.inits
Prelude.sum
[*]Redefine the sum
function which sums elements of a list of numbers.
Prelude.last
[*]Redefine the last
function which returns the last element of a list.
Prelude.init
[*]Redefine the init
function which returns all the elements of a list but the last.
Prelude.minimum
[*]Redefine the minimum
function which returns the element with the smallest value in the list.
Hint: Use the min
function which returns the smaller one from its two parameters.
Prelude.concat
[*]Redefine the concat
function which concatenates arbitrary number of lists of the same type of elements.
(Prelude.++)
[*]Redefine the (++)
operator which concatenates two lists.
Define a function to merge lists. The resulting should contain elements of both input lists ordered.
Prelude.zip
[*]Redefine the function for combining lists into a list of pairs (which is called “zipping”).
Data.List.isPrefixOf
[*]Redefine the isPrefixOf
function from the Data.List
module which determines if the second parameter starts with the first parameter (and contains it entirely). If this is true, then we say that the first parameter is the prefix of the second.
Prelude.elem
[*]Redefine the elem
function which decides a value is element of a list.
Data.List.nub
[*]Redefine the nub
function from the Data.List
module. The nub
function is to remove duplicate elements from a list.
Evaluate a polynomial given by its coefficients at a given point.
Split a list into segments of a given length.
Split a list into segments of lengths given in another list.
Define the every
function which takes every nth of a list.
Define the quicksort (where a list is partitioned into elements smaller and larger than its head).
Note: The current definition of qsort
is not efficient for ordered and inversely ordered lists.
Red arrow: Elements smaller than the head in the tail. Grey arrow: Head of the list. Blue arrow: Elements larger than the head in the tail. Green arrows: Concatenation.
For a random list:
For ordered lists, qsort
runs in linear time because the time required for concatenating the two lists is proportinal to the length of the first list.
Visually:
For inversely ordered lists, qsort
runs in quadratic time, because the time required for concatenating the two lists is proportional to the length of the first list.
Visually:
Data.List.tails
[*]Generate all possible suffices for a list. (This function is already defined in the Data.List
module.)
Data.List.inits
Generate all possible prefices for a list. (This function is already defined in the Data.List
module.)
Delete an element from a list in all possible ways.
Insert an element into a list in all possible ways.
Give all the possible permutations for a list.
[“123”,“213”,“231”,“132”,“312”,“321”]
Grey arrows: Splitting the list into head and tail. Red arrows: Permutation (recursive call). Blue arrows: Applying insertions
.
Furthermore:
Decompose an integer into sums in all possible ways.
Each sum is represented as a list (e.g. 1 + 2 + 4
is representead as [1,2,4]
).
Let the ordering matter in the sum (i.e. 1 + 2
and 2 + 1
are different decompositions).
Hint: The algorithm is as follows.
If we want to decompose the integer 4
in all possible way, then it can be done in the following way:
4
is a decomposition of 4
.3 + x
is a decomposition of 4
if x
is a decomposition of 1
. Since 1
can be only decomposed in one way, this will give us 3 + 1
.2 + x
is a decomposition of 4
if x
is a decomposition of 2
. Since decompositions of 2
are 2
and 1 + 1
, this will give us 3 + 2
and 3 + 1 + 1
.1 + x
is a decomposition of 4
if x
is a decomposition of 3
. Since decompositions of 3
are 3
, 2 + 1
, 1 + 2
, and 1 + 1 + 1
, this will give us 1 + 3
, 1 + 2 + 1
, 1 + 1 + 2
, and 1 + 1 + 1 + 1
.We called the part
function in each case. Then we concatenate the results of all the four cases.