Solutions to exercises from week 2

;;; take one argument, guaranteed to be a list of precisely two elements,
;;; and return a new list which contains both these elements but in
;;; reverse order
(defun reverse-list-of-two (list)
  (list (second list)
        (first list)))

;;; take two arguments and return them in a list, second argument first
(defun list-of-args-in-reverse-order (first second)
  (list second first))

;;; average of three arguments
(defun average-of-three (one two three)
  (/ (+ one two three) 3.0))

;;; average of three members of list
(defun average-of-three-list-members (list)
  (/ (+ (first list)
        (second list)
        (third list))
     3.0))

;;; how many minutes in one year? It's a leap year if leapp is set
(defun minutes-in-one-year (leapp)
  (if leapp
      ;; leap case
      (* 366 24 60)
    ;; non-leap case
    (* 365 24 60)))

;;; alternative solution
(defun alternative-minutes-in-one-year (leapp)
  (* (if leapp 366 365)     ;  I think this is somewhat more elegant (why?)
     24
     60))

;;; give lisp an additional ordinal to play with
(defun eleventh (list)
  (tenth (rest list)))

If you have tried the exercises, looked at the solutions and still do not understand what's going on, I am available for consultation at the times advertised on my office door. Bring your code with you in BOTH the following forms:

 

Nick Levine
                                                                               last modified 2000-10-10
                                                         Copyright (C) Nick Levine 1999. All rights reserved.
$Id: //info.ravenbrook.com/user/ndl/lisp/declarative/lectures/solutions/solutions-2.html#2 $