emacs.d/clones/lisp/www.csci.viu.ca/~wesselsd/courses/csci330/code/lisp/smallest.cl.html
2022-10-07 19:32:11 +02:00

27 lines
1 KiB
HTML

#! /usr/bin/gcl -f
; find smallest element of a list, where user specifies
; a type checking function, comparison function,
; and a "smallest so far" that defaults to a maximal value
(defun smallest (L &optional (TypeCheck 'integerp) (Compare '<) (SoFar most-positive-fixnum))
(cond
((not (listp L)) SoFar)
((not (functionp TypeCheck)) SoFar)
((not (functionp Compare)) SoFar)
((null L) SoFar)
((not (funcall TypeCheck (car L))) (smallest (cdr L) TypeCheck Compare SoFar))
((funcall Compare (car L) SoFar) (smallest (cdr L) TypeCheck Compare (car L)))
(t (smallest (cdr L) TypeCheck Compare SoFar))))
; try out with the defaults
(defvar NumList '(10 19 5 11 8))
(format t "Smallest value in ~A is: ~A~%" NumList (smallest NumList))
; try it out on a list of strings
(defvar StrList '("hello" "there" "whoever" "you" "are" "now"))
(defvar typ 'stringp)
(defvar comp 'string<)
(defvar max "zzzzzzzzzzz")
(format t "Smallest value in ~A is: ~A~%" StrList (smallest StrList typ comp max))