Review: Week1

  1. ELIZA:
    1. Assume the following conversation patterns:
      (((?* ?x) I am sad (?* ?y))
       (I am sorry to hear you are depressed)
       (I'm sure its not pleasant to be sad))
      
      (((?* ?x) are like (?* ?y))   
       (What resemblance do you see between ?x and ?y))
      
      (((?* ?x) is like (?* ?y))    
       (In what way is it that ?x is like ?y)
       (What resemblance do you see?)
       (Could there really be some connection?) 
       (How?))
      

      Write down 5 rounds of patient/ doctor dialogue supported by these patterns.

  2. LISP:
    1. Write a function to exponentiate , or raise a number to an integer power.
      For example: (power 3 2)= 3*3 = 9
      Do it three ways (and, for a hint, see here)
      1. Do it using a looping construct (no recursion).
      2. Do it using a recursive call to the same top-level function
      3. Do it using a helper function defined inside the top-level function.

    2. Write a function that counts the number of times an expression occurs anywhere within another expression. Example:
      (count-anywhere 'a '(a ((a) b) a)) ==> 3
      Hint: your function should recurse on all parts of the list.
       
    3. Consider the following function:
      1. what do apply, append, mapcar do?
        (defun mappend (fn list)
          (apply #'append (mapcar fn list)))
        
      2. What is the output of this code?
        (defun numbers-and-negations (input)
            (mappend #'number-and-negation input))
        
        (defun number-and-negation (x)
          "If x is a number, return a list of x and -x."
            (if (numberp x)
             (list x (- x))
                nil))
        
    4. Define and distinquish the mean and median of a list of numbers. Implement a LISP predicate that returns the median of a list of numbers. (Here's a solution to this question- but try to answer it first before you look at the answer.)
    5. The following list code implements several useful LISP idioms. Describe those idioms:
      (defun ordered (l &key (predicate #'<) (key #'identity))
           (stable-sort (copy-list l) predicate :key key))
      
    6. The above code could be replaced with the following code. Describe the differences between this code and the code in (1a).
      (defmacro ordered (l &key (predicate #'<) (key #'identity))
          `(sort ,l ,predicate :key ,key))