Haskell Programming: Confused on how to start and solve this problem. Any guidan
ID: 3715659 • Letter: H
Question
Haskell Programming: Confused on how to start and solve this problem. Any guidance or help would be great
1. (5 points) The myfoldr and mylengthr are defined in Haskell as follows: myfoldr:: (a -> b -> b) ->b-> [a] - b myfoldr f acc acc myfoldr f acc (x:xs) f x (myfoldr f acc xs) mylengthr :: [a] -Int mylengthr -myfoldr (- n -> 1 + n) o Show the evaluation steps of mylengthr [1,2,3,4] -> ">4 2. (5 points) The myfoldl is defined in Haskell as follows: myfoldl : (a -> b -> a) -> a -> [b]-a myfoldl f acc acc myfoldl f acc (x:xs) = myfoldl f (f acc x) xs Write a function called mylengthl using myfoldl. The mylengthl should output the length of a given list. 3. (a) (5 points) Write a function called myreverse using myfoldl. The myreverse should output the reverse of a given list b) (5 points) Show the evaluation steps of myreverse [1,2,3,4] -> [4,3,2,1].Explanation / Answer
First of all I'would like to explain what these expressions mean in case if you don't know.
The first line is: myfoldr :: (a -> b -> b) -> b -> [a] -> b
This means a function myfoldr is defined which has three arguments viz. a function, a number, a list and returns a number.
(a -> b -> b) is the first argument which itself is a function taking two arguments of type number a and b, and returns another number.
b is the second argument which says the type is a number
[a] is the third argument which defines the type as list
finally, b is the type of the output returned by the function. Here, it is a number.
Now, consider the second line:
myfoldr f acc [] = acc
This implies myfoldr with 'f'as the function, acc as the accmulator number and [] as the empty list, returns the acc number. This is the boundary condition.
The third line writes:
myfoldr f acc (x:xs) = f x (myfoldr f acc xs)
This implies that myfoldr with 'f'as the function, acc as the accumulator number and (x:xs) as the list is equal to the value of the function 'f'applied on arguments x and the output of (myfoldr f acc xs).
Now, with these explainations, writing the solutions below:
1. mylengthr [1,2,3,4] => myfoldr (_ n -> 1+n) 0 [1,2,3,4]
=> f 1 (myfoldr f 0 [2,3,4])
=> f 1 (f 2 (myfoldr f 0 [3,4]))
=> f 1 (f 2 (f 3 (myfoldr f 0 [4])))
=> f 1 (f 2 (f 3 (f 0 (myfoldr f 0 []))))
=> f 1 (f 2 (f 3 (f 0 (0))))
=> f 1 (f 2 (f 3 (1)))
Consider the lambda function here: f = (_ n -> 1 + n).
This means a lambda function is defined which takes two arguments (_, n). '_'means we don't care about
the first argument. second argument is n. Result of th function is n+1.
So, output of f 0 0 = 0+1 = 1
f 3 1 = 1 +1 = 2
f 2 2 = 1 + 2 = 3
f 1 3 = 1 + 3 = 4
Hence, the result is 4.
2. mylengthl :: [a] -> Int
mylengthl = myfoldl ( _ -> n + 1) 0
Here, function is lambda function which is defined as ( _ -> n + 1). This means it takes two arguments, first one n, the second one we don't care, and returns the value as n+1. So, f 0 0 = 1, f 1 0 = 1, and so on.
Let's see the case for list [1,2,3,4]
mylengthr [1,2,3,4] => myfoldl ( _ -> n + 1) 0 [1,2,3,4]
=> myfoldl f (f 0 1) [2,3,4]
=> myfoldl f 1 [2,3,4]
=> myfoldl f (f 1 2) [3,4]
=> myfoldl f 2 [3,4]
=> myfoldl f (f 2 3) [4]
=> myfoldl f 3 [4]
=> myfoldl f (f 3 4) []
=> myfoldl f 4 []
=> 4
3.
(a) myreverse :: [a] -> [a]
myreverse = myfoldl (x xs -> (xs:x)) []
Lambda function here takes two arguments x and xs, and append the number xs at the head of the first argument x.
(b) myreverse [1,2,3,4] = myfoldl f [] [1,2,3,4]
=> myfoldl f (f [] 1) [2,3,4]
=> myfoldl f [1] [2,3,4]
=> myfoldl f (f [1] 2) [3,4]
=> myfoldl f [2,1] [3,4]
=> myfoldl f (f [2,1] 3) [4]
=> myfoldl f [3,2,1] [4]
=> myfoldl f (f [3,2,1] 4) []
=> myfoldl f [4,3,2,1] []
=> [4,3,2,1]
Please let us know if any help is needed regarding the clarity of the solution.
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.