Jotter <2026-03-19 Thu>

(defun test-sum-to ()
  (and (= 6 (sum-to 3))
       (= 0 (sum-to 0))
       (= 1 (sum-to 1))
       (= 10 (sum-to 4))))

(defun sum-to (n)
  "Add up from 1 to N, where N is a non-negative integer."
  (cond ((zerop n) 0)
        ((plusp n)
         (+ n (sum-to (- n 1))))))

(defun sum-to (n &optional (acc 0))
  "Add up from 1 to N, where N is a non-negative integer."
  (cond ((zerop n) acc)
        ((plusp n)
         (sum-to (- n 1) (+ n acc)))))

(defun test-coddp ()
  (and (eq nil (coddp 2))
       (eq nil (coddp 0))
       (eq t (coddp 1))
       (eq nil (coddp 10))
       (eq t (coddp 9))))

(defun coddp (n)
  "Determine whether the natural number N is odd."
  (cond ((zerop n) nil)
        ((plusp n) (cevenp (- n 1)))))

(defun cevenp (n)
  "Determine whether the natural number N is even."
  (cond ((zerop n) t)
        ((plusp n) (coddp (- n 1)))))

(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))))))))

(defun foo (a &optional (b 33))
  (list 999 a b))


Author: Breanndán Ó Nualláin

Created: 2026-03-19 Thu 12:49

Validate