Academic Integrity: tutoring, explanations, and feedback — we don’t complete graded work or submit on a student’s behalf.

-Could you please answer this question by using haskell programming language and

ID: 3591557 • Letter: #

Question

-Could you please answer this question by using haskell programming language and do a test for each function .?

1-Suppose you need a Haskell function sumSqNeg that takes a list of integers (type Integer) and returns the sum of the squares of the negative values in the list. Define the following Haskell functions. Use the higher order library functions such as map, filter, foldr, and foldl as appropriate.

a)Function sumSqNeg1 that is backward recursive. (Use recursion directly. Do not use the list-folding Prelude functions such as foldr or sum.)

b)Function sumSqNeg2 that is tail recursive. (Use recursion directly. Do not use the list-folding Prelude functions such as foldr or sum.)

c)Function sumSqNeg3 that uses standard prelude functions such as map, filter, foldr, and foldl.

d)Function sumSqNeg4 that uses list comprehensions .

Explanation / Answer

A tuple is a fixed-length coupling of values, written in parentheses with the values separated by commas. One way to use this is to pass all parameters into a function as one value, rather than the curried functions we've seen so far.

max3 :: (Double, Double, Double) -> Double
max3 (x, y, z) = max (max x y) z

To call this max3 function, we'd need to pass the full tuple of three values as a parameter: “max3 (12, 5, 13)”. Since max3 isn't curried, we can no longer partially call the function with parameters.

Of course, this brings up the question: Why would you prefer tuples as parameters rather than currying the function? There is no definitive answer to this; while most Haskell programmers prefer currying, some indeed prefer tuples. There are a few reasons for preferring tuples: it enhances readability (in the opinion of some), it enables a compiler to catch errors with passing the wrong number of arguments more immediately, and it could improve performance marginally — though one should never underestimate the power of a compiler to figure out the best way to execute any code. None of these reasons are so strong, though, as to convince a majority of Haskell programmers that they should avoid curried functions.

But tuples are useful in other contexts, too. One such use is for returning multiple values from a function. The following function, for instance, finds the minimum and maximum values of a function f within a range (a, b) by simply trying different x-values delta apart. It returns both the minimum and the maximum encoded in a tuple.

A list is a singly linked list like one sees in an imperative language, but with one important difference: We cannot change any values within a list, including the pointers from one list node to another. The only operation we have available is to insert a node at the beginning of the list.

A list in Haskell can be written using square brackets with commas separating the list's individual values. Thus, the expression “[2,3,5]” represents a list with three values, of which the first is 2, the second is 3, and the third is 5. Haskell also allows expressing a list of successive values, as in “[10..20]” containing the eleven integers from 10 to 20. Naturally, the empty list would be written “[].”

To write functions working with lists, we can use four fundamental operations:

null lst

Returns true if lst is empty.

head lst

Returns the first value of lst.

tail lst

Returns the list of values from lst following its first value.

v : lst

Returns a list beginning with v and followed with the values of lst.

We can easily use these to write our own list functions that iterate through the list to arrive at their result. For example, we might want to create a function that counts the number of times a particular integer occurs in a list.