CS 3304 Homework #5

Date Assigned: October 8, 2003
Date Due: October 13, 2003, in class, before class starts
  1. (20 points) Write the fibonacci function, fib, in ML with only one (tail) recursive call (not two). (HINT: requires only 5 lines of code).

  2. (10 points) Write a ML function, remove, that, given a list L and an integer i, returns a copy of L with the ith element removed. If the length of L is less than i, return L.

  3. (10 points) Write a higher-order function called lreduce in ML that takes a two-parameter function F and a list [a1,a2,...,an] as arguments, and produces:

    F(...F(F(a1,a2),a3)...,an)

    Explain one application of lreduce. i.e., show how you will use it to do something useful.

  4. (10 points) Using only the higher-order functions map, foldr, and foldl (and maybe function composition), write the following functions in ML:

    • A function, asum that turns a list of integers [a1,a2,..,an] into the alternating sum a1-a2+a3-a4+....

    • A function, xor, that computes the XOR of a list of booleans.

    You must use one or more of the above higher-order functions to solve this exercise.

Return Home