CSE 341
Assignment 3: Haskell

Due on Wednesday, July 14, 1999

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:

  1. (a Haskell version of Assignment 1, No. 2) a function called squareList which takes a list of numbers and returns a list of the squares of the numbers

    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]
    [1,16,4,0,9,9]

    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.

  2. (a Haskell version of Assignment 1, No. 3) a function called filterGE which takes a number k and a list of numbers and returns a list containing only those list elements which are greater than or equal to k (again, in the same order)

    example usage:

    Main> filterGE 2 [1,-2,4,0,3,3,1,2]
    [4,3,3,2]

  3. a function called factors which takes a positive integer k and returns a complete list of all integers greater than 1 which divide k

    example usage:

    Main> factors 12
    [2,3,4,6,12]

  4. a function called primes that returns an infinite list of prime numbers

    example usage:

    Main> take 20 primes
    [2,3,5,7,11,13,17,19,23,29,31,37,41,43,47,53,59,61,67,71]

    note: 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?

    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.

  5. functions as specified in Sethi, Ex. 9.5 on p. 380, parts (a) through (d), with the following additional specifications:

    (a) Name the function isMember and make it take a list and an item an item and a list as parameters in that order.

    (b) Name the function union.

    (c) Name the function intersection.

    (d) Name the function difference.

    Note 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.

    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


Back to the 341 home page...
Last modified: Sat Jul 10 14:11:42 PDT 1999