CS 250 - Programming Languages
Spring 2001

Assignment 4 -- Due April 9 at 10:30am.

You must turn in the answers to these questions both in hardcopy and by e-mail to echown@bowdoin.edu.

Demonstrate that your code works by including adequate examples, including all those presented here. There should not be an assignment form (setq or setf) anywhere in the functions you define.

  1. Write a function fringe that takes a list as an argument and returns a list whose elements are all the atoms appearing in the original list or any of its sublists, arranged in left-to-right order. For example,

    > (setf x (cons (list 1 2) (list 3 4)))
    ((1 2) 3 4)
    >(fringe x)
    (1 2 3 4)
    >(fringe (list x x))
    (1 2 3 4 1 2 3 4)
    
  2. Write a function, called wheres-waldo, that takes a lisp object as its argument and returns a Lisp expression that extracts the symbol waldo from this object, if it is present. For example,

    ? (wheres-waldo '(ralph waldo emerson))
    (FIRST (REST '(RALPH WALDO EMERSON))
    
    Note that if we evaluate this expression we get waldo,

    ? (first (rest '(ralph waldo emerson)))
    WALDO
    
    In general, (eval (wheres-waldo x)) should return waldo if waldo is anywhere in x, and nil otherwise. Some more examples:

    ? (wheres-waldo 
         '(mentor (ralph waldo emerson) (henry david thoreau)))
    (FIRST (REST (FIRST (REST
                 '(MENTOR (RALPH WALDO EMERSON)
                          (HENRY DAVID THOREAU))))))
    ? (wheres-waldo '(henry david thoreau))
    NIL
    ? (wheres-waldo
       '((lives-at (henry david thoreau) (walden pond))
         (shops-at (john q tourist) (wall drug))
         (shops-at (henry david thoreau) (wal mart))
         (motivation (jane q 250 student) (will do))
         (cooks-meat (henry david thoreau) (well done))
         (visits (henry david thoreau)
                 (home-of (wally waldo waldovsky)))
         (eats (mr ed) (wild oats))))
    (FIRST (REST (FIRST (REST (FIRST (REST (REST (FIRST (REST . . .
    
  3. Write a generalized version of the above (call it wheres-target) that takes a general target lisp object and finds it in the given source. For example,

    ? (wheres-target '(waldo emerson) '(ralph waldo emerson))
    (REST '(RALPH WALDO EMERSON))
    ? (wheres-target '(ralph waldo) '(ralph waldo emerson))
    NIL
    ?(wheres-target '(whale dough)
                    '(((waldo) (whale doe))
                      (((wale do) (wailed o (whale (dough)) whale dough)))))
    (REST (REST (REST (FIRST (REST (FIRST (FIRST (REST '(((WAL. . .
    
  4. Write a new version of wheres-waldo, in terms of wheres-target.