Jotter <2026-03-30 Mon>
(defun test-isort ()
(and (equal () (isort ()))
(equal '(5) (isort '(5)))
(equal '(-1 -0.5 0 1) (isort '(-1 1 0 -0.5)))
(equal (list 1 (sqrt 2) 1.5 2)
(isort (list (sqrt 2) 1 2 1.5)))
(equal '(1) (isort '(1 1 1)))
(equal '(1 2) (isort '(1 1 2 2)))
(equal (list pi 3.145)
(isort (list pi 3.145)))))
(defun isort (ns)
"Sort the list of real number NS in increasing order."
(cond ((null ns) ())
((consp ns) (insert (first ns) (isort (rest ns))))))
(defun test-insert ()
(and (equal '(0) (insert 0 ()))
(equal '(2 3 4) (insert 3 '(2 4)))
(equal '(1 2) (insert 2 '(1 2)))
(equal '(-1 1 2) (insert 1 '(-1 2)))
(equal '(0 0.5 1) (insert 0.5 '(0 1)))
(equal '(2 3 4 5) (insert 2 '(3 4 5)))
(equal '(3 4 5 6) (insert 6 '(3 4 5)))))
(defun insert (n ns)
"Insert the real number N into the sorted (increasing) ist of real numbers NS."
(cond ((null ns) (list n))
((consp ns)
(cond ((< n (first ns)) (cons n ns))
((= n (first ns)) ns)
((> n (first ns))
(cons (first ns) (insert n (rest ns))))))))
;; An atom is a number, a symbol or a string. (Note that Common Lisp has a
;; broader definition of an atom but ours will suffice for these exercises.)
;; We define an s-expression to be anything which is an atom or a cons of two
;; s-expressions.
(defun atomp (x)
"Determines whether X is an atom, i.e. a number, symbol or string."
(or (numberp x)
(symbolp x)
(stringp x)))
(defun sexp-p-test ()
(and (sexp-p (list 1 2))
(sexp-p '(1 2 3))
(sexp-p (cons '(1 2 3) (list 1 2)))
(sexp-p (cons 1 2))
(sexp-p (cons 3 nil))
(sexp-p '(cons 3 nil))
(sexp-p '(defun atomp (x)
"Determines whether X is an atom, i.e. a number, symbol or string."
(or (numberp x)
(symbolp x)
(stringp x))))))
(sexp-p-test) T
(defun sexp-p (x)
"Determines whether X is an s-expression."
(or (atomp x)
(and (consp x)
(sexp-p (first x))
(sexp-p (rest x)))))
(defun sexp-sum-numbers-test ()
(or (= 6 (sexp-sum-numbers '(year 1 year 2 year 3)))
(= 0 (sexp-sum-numbers ()))
(= 0 (sexp-sum-numbers ""))
(= 0 (sexp-sum-numbers '(year1 year2 year3)))
(= 15 (sexp-sum-numbers '(3 4 8)))
(= 5 (sexp-sum-numbers '((((((((((((5))))))))))))))
(= 12 (sexp-sum-numbers '((3) (4) (5) ("abc") "34")))
(= 0 (sexp-sum-numbers '(defun atomp (x)
"Determines whether X is an atom, i.e. a number, symbol or string."
(or (numberp x)
(symbolp x)
(stringp x)))))
(= 2 (sexp-sum-numbers '(defun fact (n)
"The factorial function"
(if (zerop n)
1
(* n (fact (- n 1)))))))))
(sexp-sum-numbers-test) T
(sexp-sum-numbers nil) 0
(defun sexp-sum-numbers (sexp)
"Takes an s-expression SEXP and returns the sum of all the numbers occurring in
it"
(cond ((numberp sexp) sexp)
((atomp sexp) 0)
(t ;; it's a cons of sexps
(+ (sexp-sum-numbers (first sexp))
(sexp-sum-numbers (rest sexp))))))
(defun sexp-sum-numbers (sexp)
"Takes an s-expression SEXP and returns the sum of all the numbers occurring in
it"
(cond ((numberp sexp) sexp)
((atomp sexp) 0)
(t ;; it's a cons of sexps
(+ (sexp-sum-numbers (first sexp))
(sexp-sum-numbers (rest sexp))))))
(random-student)
(absent)
(insert-bst 2.5 bst)
Author: Breanndán Ó Nualláin <o@uva.nl>
Date: 2026-03-30 Mon 17:53