Programming Languages assignment VII
Scheme discussed rather briefly in Sec. 11.2.1 of Scott's book. You can ignore the section on assignment,as well as the section on evaluation order and monads. On the other hand, section 11.2.3 on higher-order functions highlights one of the most interesting aspects of functional programming, and you should understand its contents. To use Scheme on the machines at NYU, or download a version of Scheme for your machine, see the pointers next to lecture 11 in the course outline.
Do the following exercises in Scheme: a) Write a function that computes the length of a list, counting its components at the top level: (len '(1 2 4 6 8)) yields 5 (len '( (one 1) (two 2))) yields 2 b) Write a function that computes the number of atoms in a list: (len '( (one 1) ((two 2)))) yields 4 c) Suppose you have a list of pairs (an association list) in which symbols are paired with an integer (a count). For example: ((Mark 15) (Angela 27) (Joe 12) (Curly 23)) Write a function that takes such a list, and a symbol. If the symbol is already present in the list, return a list with the corresponding count updated. (Update 'Joe '((Mark 15) (Angela 27) (Joe 12) (Curly 23))) yields ((Mark 15) (Angela 27) (Joe 13) (Curly 23)) If the symbol is not present in the list, the function adds the association (Symb 1) to the list. d) Using the function you wrote in part c) write a function that takes a list with duplicate symbols, and produces an association list where each symbol is associated with the number of times it appears in the original list. So for example: (catalog '(a b c a b b x y a x b)) yields ((a 3) (b 4) (c 1) (x 2) (y 1))