; Examples showing atoms and the cons/2 function nil 1 (cons 1 nil) (cons nil nil) (cons 'a nil) (cons 'a 'b) (cons 1 (cons 2 (cons 3 nil))) ; (A B) '(A B) (cons 'C '(A B)) ; The following lets me re-read this file easily (defun again () (require "examples.lisp")) ; Shows how to define a function (defun consSelf (L) ; L is expected to be a list (cons L L) ) ; Sample uses of consSelf/1 (consSelf 1) (consSelf nil) (consSelf '(A B)) ; Shows how to use a conditional (defun test1 (A) (cond ((eq A 1) "yes") (T "no") ) ) ; Sample uses of test1/1 (test1 1) (test1 '1) (test1 'a) (test1 2) (test1 (cons 1 nil)) ; The car/1 and cdr/1 functions (car '(a b)) (cdr '(a b)) (car '(1)) (cdr '(1)) (car '(a . b)) (cdr '(a . b)) ; Shows how to make a recursive function (defun lastElt (L) (cond ((null (cdr L)) (car L)) ; L is a one-element list ; ((null L) nil) ; L is empty: no last element (T (lastElt (cdr L))) ; recurse ) ) ; Sample uses of lastElt/1 (lastElt nil) ; (lastElt 1) (lastElt '(1)) (lastElt '(1 2 3)) (lastElt '(1 (2 2) 3)) (lastElt '(1 2 (3 3))) ; Arithmetic (defun incrementAll (L) (cond ((null L) nil) ; L is empty (T (cons (+ 1 (car L)) (incrementAll (cdr L)))) ) ) ; Tests of incrementAll/1 (incrementAll nil) ; (incrementAll 1) (incrementAll '(1)) (incrementAll '(1 3 5)) ; (incrementAll '(1 (3 3) 5)) ; Function with two parameters (defun incrementAll2 (L A) ; increment all elements of L by amount A (cond ((null L) nil) ; L is empty (T (cons (+ A (car L)) (incrementAll2 (cdr L) A))) ) ) ; Tests of incrementAll2/2 ; (incrementAll2 nil) (incrementAll2 nil 5) (incrementAll2 '(1) 5) (incrementAll2 '(1 3 5) 5) (defun ReverseIt (L) (cond ((null L) nil) (T (append (ReverseIt (cdr L)) (cons (car L) nil))) ) ) ; Tests of ReverseIt/1 (ReverseIt nil) (ReverseIt '(a b)) (ReverseIt '(a (b c) d))