Don't put away your recursion thinking caps! It's time for more functional programming, y'all, but with new language features like pattern matching, infinite lists, and list comprehension.
Please note the correction in No. 5, Part (a).
Write and test these functions using the Hugs98 Haskell 98 environment:
Ordering should be preserved, i.e. the numbers in the returned list should be in the same order as in the original list.
Give two implementations of this function: The first using map and the second without using it. Just as in Scheme, map takes a function and a list and applies the function to each member of the list. For the version using map, try writing it as a higher-order function, i.e. such that squareList doesn't take any parameters and returns a function that takes a list as its single parameter.
example usage:
Main> squareList [1,4,-2,0,3,3]hint: Don't spend time worrying you've done something wrong if your functions are only a line or two long. Sit back, marvel at Haskell's expressiveness, and move on to No. 2.
[1,16,4,0,9,9]
example usage:
Main> filterGE 2 [1,-2,4,0,3,3,1,2]
[4,3,3,2]
example usage:
Main> factors 12
[2,3,4,6,12]
example usage:
Main> take 20 primesnote: You may find take useful for testing. This function takes an integer k and a list and returns the first k items of the list. See how this might be nice for infinite lists?
[2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71]
hint: Try using list comprehension and the factors
function you defined above.
another hint: Each of factors and primes can
be written in one, short, beautiful line.
(a) Name the function isMember and make it takeNote that (b) through (d) should all take two lists as parameters. Items in the returned lists do not have to be in any particular order.a list and an iteman item and a list as parameters in that order.(b) Name the function union.
(c) Name the function intersection.
(d) Name the function difference.
example usages:
Main> intersection [2,3,45,1] [1,3,8,99]
[3,1]Main> difference [2,3,45,1] [1,3,8,99]
[2,45]Main> union [1,3,2] [7,3,5,9,1]
[2,7,3,5,9,1]
Main> union [] [3,2]
[3,2]Main> isMember 5 [2,3,4,-2,5]
True
Main> isMember 7 [2,3,4,-2,5]
False