Exercises #1

  1. Write the identity function (the I combinator), which simply returns its argument (any type of argument) unchanged:
    # I :: alpha -> alpha
    proc I {x} {...}
    Examples:
    I 12
    => 12
    I foo
    => foo
    I {string length abracadabra}
    => {string length abracadabra}

    Why is this a useful function?

  2. Write the K combinator, which takes two arguments and always returns its first argument, unchanged:
    # K :: alpha beta -> alpha
    proc K {x y} {...}
    Examples:
    K 0 456
    => 0
    K foo 1
    => foo

    Why is this a useful function?

  3. Write the function change, which takes a whole number as it argument and returns a list that represents how to "make change" for that number, taken as a number of cents. You can use quarters, dimes, nickels and pennies to make change, and so the result list should be of length four, sorted from most valuable coin to least valuable. You should make change using as few coins as possible.
    # change :: num -> [num]
    proc change {n} {...}
    change 75
    => 3 0 0 0
    change 23
    => 0 2 0 3
  4. Write the iota function, which takes a numeric argument n and returns a list of numbers of length n which are the numbers from 0 to n-1.
    # iota :: num -> [num]
    proc iota {n} {...}
    Examples:
    iota 3
    => 0 1 2
    iota 20
    => 0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19

    Does your function work for 0? What should your function do for negative arguments?

  5. Write the function incrlist, which takes one argument, a list of numbers, and returns a list of equal length as a result, in which each element is the successor to the corresponding element of the argument list.
    # incrlist :: [num] -> [num]
    proc incrlist {L} {...}
    incrlist {34 987 1 567 -23 8}
    => 35 988 2 568 -22 9
    incrlist [iota 12]
    => 1 2 3 4 5 6 7 8 9 10 11 12

    Does your function work for the empty list?

  6. Write the function strlenlist, which takes one argument, a list of strings, and returns a list of equal length as a result, in which each element is the string length of the corresponding element of the argument list.
    # strlenlist :: [str] -> [num]
    proc strlenlist {L} {...}
    strlenlist {34 987 1 567 -23 8}
    => 2 3 1 3 3 1
    strlenlist {foo bar antidisestablishmentarianism}
    => 3 3 28

    Does your function work for the empty list?

  7. Write sumlist, which takes one argument, a list of numbers, and returns, as result, a single number: the sum of the numbers in the argument list.
    # sumlist :: [num] -> num
    proc sumlist {L} {...}
    sumlist [iota 3]
    => 3
    sumlist {34 987 1 567 -23 8}
    => 1574
  8. Write multlist, which takes one argument, a list of numbers, and returns, as result, a single number: the product of the numbers in the argument list.
    # multlist :: [num] -> num
    proc multlist {L} {...}
    multlist [iota 3]
    => 0
    multlist [incrlist [iota 3]]
    => 3
    multlist {34 987 1 567 -23 8}
    => 793928272
  9. Write catlist, which takes one argument, a list of strings, and returns, as result, a single string: the concatenation of the strings in the argument list.
    # catlist :: [str] -> str
    proc catlist {L} {...}
    catlist [iota 3]
    => 012
    catlist [incrlist [iota 3]]
    => 123
    list {foo bar antidisestablishmentarianism}
    => foobarantidisestablishmentarianism
  10. Write factorial, iteratively.
    # factorial :: num -> num
    proc fact {num} {...}
    factorial 3
    => 6
    factorial 6
    => 720
    factorial 12
    => 479001600
  11. Write factorial, recursively.
  12. Write factorial, neither iteratively nor recursively, using only functions that you've written in this set of exercises.

Keith Waclena
The University of Chicago Library
This page last updated: Fri Jul 29 11:57:23 CDT 1994
This page was generated from Extended HTML by xhtml.