Solutions to exercises from week 4

(defun matches-first-p (list number)
  (= number (first list)))

(defun matches-any (list number)
  (if list
      (if (matches-first-p list number)
        (matches-first-p (rest list) number))))

(defun position-for-matches-any (list number)
  (in-position-for-matches-any list number 0))

;; helper function for position-for-matches-any
(defun in-position-for-matches-any (list number count)
  (if list
      (if (matches-first-p list number)
        (in-position-for-matches-any (rest list) number (+ 1 count)))))

;; in terms of car and cdr...
(defun my-fourth (list)
  (car (cdr (cdr (cdr list) ))))

(defun max-of-two-args (first second)
  (if (> first second)

(defun print-n-dots (n)
  (if (> n 0)
      (let* ((new (1- n)))
        (format t ".")
        (print-n-dots new))))

;; You might be correct in thinking this coding style looks a little stilted.
;; We'll be able to do a cleaner job after this week...
(defun count-symbol-a (list)
  (if list
      (let* ((this (if (equal (first list) 'a)
        (+ this
           (count-symbol-a (rest list))))

;; needed correcting: there was no termination condition
(defun summit (list)
  (if list
      (let* ((x (car list)))
        (if (null x)
            (summit (cdr list))
          (+ x (summit (cdr list)))))

(defun better-maximum (list)
  ;; nick 2000-10-24 check for empty starting list!
  (if list
      (in-better-maximum (first list) (rest list))))

;; helper function for better-maximum
(defun in-better-maximum (previous-best list)
  (if list
      (let* ((next (first list))
             (rest (rest list)))
        (in-better-maximum (max-of-two-args next previous-best) rest))

(defun final-cons (list)
  (car (last 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-24
                                                         Copyright (C) Nick Levine 1999. All rights reserved.
$Id: // $