I am struggling to think of a way to utilize these functions for this beginner level coding class that I am taking to learn functional programming in Haskell. The functions I have to write are shown below, asum is supposed to turn a list of integers [a1,a2,..,an] into the alternating sum a1-a2+a3-a4+.… and I am not sure how to approach it with these functions. The xor function is supposed to that computes the XOR of a list of Booleans. I need some help to understand how to use these functions and it would greatly appreciated. I am also new to Haskell so any explanations would help. Thanks I have to use map foldr foldl.
asum :: (Num a) => [a] -> a
xor :: [Bool] -> Bool
Here's an idea:
This fits pretty well to the
foldrpattern of recursion,So it can be coded by setting
f = ...andz = ...and callingYou will need to complete this definition.
For the
XORof a list of Booleans, assuming it is to beTrueif one and only one of them isTrue, andFalseotherwise, we can imagine this sequence of transformations:where
tandfare some specially chosen numbers. And then we can find the sum of this second list (not alternating sum, just a sum of a list of numbers) and check whether it is equal to ... some (other?) special number, let's call itn1:fun1is the function which transforms each element of its argument list according to the given function, calledtransformabove. It is one of the two functions left from the three you were given, considering we've already been usingfoldr.sumis to be implemented by using the last function that's left.FYI,
and