示例:实用函数(Utilities)

(defun single? (lst)
       (and (consp lst)
        (null (cdr lst))))

CL-USER> (single? nil)
NIL
CL-USER> (single? '(a))
T

(defun append1 (lst obj)
       (append lst (list obj)))

CL-USER> (append1 '(a b c d) 'e)
(A B C D E)

(defun map-int (fn n)
       (let ((acc nil))
         (dotimes (i n)
           (push (funcall fn i) acc))
         (nreverse acc)))
CL-USER> (map-int #'(lambda (x) (+ x 1)) 5)

(1 2 3 4 5)

(defun filter (fn lst)
       (let ((acc nil))
         (dolist (x lst)
           (let ((val (funcall fn x)))
         (if val (push val acc))))
         (nreverse acc)))
CL-USER> (filter #'(lambda (x) (+ x 1)) '(1 2 3 4 5))
(2 3 4 5 6)
CL-USER> (filter #'(lambda (x)
             (and (evenp x) (+ x 10)))
         '(1 2 3 4 5))
(12 14)

CL-USER> (defun most (fn lst)
       (if (null lst)
           (values nil nil)
           (let* ((wins (car lst))
              (max (funcall fn wins)))
         (dolist (obj (cdr lst))
           (let ((score (funcall fn obj)))
             (when (> score max)
               (setf wins obj
                 max score))))
         (values wins max))))
MOST
CL-USER> (most #'length '((a b) (a b c) (a)))
(A B C)
3

原文地址:https://www.cnblogs.com/flowjacky/p/2827181.html