You have to implement exercises working over lists
stack ghci
There you can try the functions, like
> member 10 [1..12]
Should print:
True
stack test
Suppose that lists describe a Set of elements.
The member
and union
functions are defined as examples.
You need to define the intersection
and difference
operations
> intersection [1..5] [7, 3, 1]
[1, 3]
> difference [1..5] [7, 3, 1]
[2,4,5]
> difference [7, 3, 1] [1..5]
[7]
Implement an operation to insert an element in the right position on an ordered list.
You should keep duplicated elements.
> insert 4 [1,2,5,7]
[1,2,4,5,7]
> insert 2 [1,2,5,7]
[1, 2, 2, 5, 7]
Based on the above implement an insertion sort.
> insertionSort [6, 3, 5]
[3,5,6]
Reimplement it using a foldr
First define a function that gets a list of Binary digits.
And gets the value of it as an Integer
Remember that if the number is:
xn ... x1 x0
x0 2 0 + x1 2 1 + ... + xn 2 n
> binaryToDecimal [1, 1, 0, 1]
13
Hint: Use an auxiliary function that receives the list in reverse order.
Create an adicional function that receives the base as an argument:
> toDecimal 8 [7, 3]
59
> toDecimal 16 [1, 1]
17
Create a function that instead of receiving a list
receives a string
> toDec 8 "73"
59
> toDec 16 "1F"
31
Hint use map
and the function digitToInt
from Data.Char
Repeat the last exercise using a List Comprehension and the zip
function
> decimal 8 "73"
59
> decimal 16 "1F"
31
Given a List, return a Nested List containing:
The first element, the first 2 elements, the first 3 elements, etc.
> firsts [1, 3, 5]
[[1],[1,3],[1,3,5]]
> firsts "Hello"
["H","He","Hel","Hell","Hello"]
Given two Strings
that represents numbers in binary.
Implement the 'binaryAdd' function.
DO NOT USE any predefined arithmetic operation
> binaryAdd "10" "1"
"3"
> binaryAdd "1111" "11"
"10010"