Lecture #2: In Class Activity

Solution Key


Problem 1

This strange function returns true if the symbol 'nil is an element in the list. Note that the empty list can be an element of another list, just like the list '(1 2) can. So the following are pefectly valid lists:

'()
'(1)
'(1 2)
'(1 nil 3 4)
'(nil)


Problem 2

  1. car (This will suitably give us the correct piece of the list).
  2. or (Or is a lazy evaluator -- it will never evaluate it's second argument, instead it will just return the first true argument, 13.
    quote (Quote ignores extra arguments, and will simply return the symbol 13, which looks the same as 13)


Problem 3

(defun dots (x)
 (or (= x 0)
     (format t ".")
     (dots (- x 1))
 )
)


Problem 4


(defun triangle-info (a b)
 (let ((a-sq (* a a))
       (b-sq (* b b))
      )
  (let ((total (+ a-sq b-sq)))
   (let ((hypo (sqrt total)))
    (format t 
            "~%A-sq:~A~%B-sq:~A~%Total:~A~%Hypotenuse:~A~%"
            a-sq
            b-sq
            total
            hypo
    )
   )
  )
 )
)

(defun triangle-info2 (a b)
 (let* ((a-sq (* a a))
        (b-sq (* b b))
        (total (+ a-sq b-sq))
        (hypo (sqrt total))
       )
  (format t 
          "~%A-sq:~A~%B-sq:~A~%Total:~A~%Hypotenuse:~A~%"
          a-sq
          b-sq
          total
          hypo  
  )
 )
)


Problem 5

See Activity #3 for solution.


Back to main page...