Lisp code: find minimum element in a list

How can we find the minimum element in a list in Lisp?

Given a large number m as an argument, get a minimum value from a list. The following code returns the minimum element in a list.

(defun get-min (m l)
; takes a list 
; return the minimum value
  (cond ((null (car l)) m)
        ((< (car l) m) (get-min (car l) (cdr l)))
        (t (get-min m  (cdr l))))
)

Update: “apply” can be used in this case, here is a better one.

(defun smallest (l)
; takes a list 
; return the smallest element
  (apply 'min l)
)

Leave a Comment