随笔分类 - <ansi Common Lisp>
摘要:mapcar:接受一个函数以及一个或多个列表,并返回把函数应用至每个列表的元素的结果,知道有的列表没有元素为止,结果为各次执行的返回值的汇总列表: CL-USER> (mapcar #'(lambda (x) (+ x 10)) '(1 2 3))(11 12 13)CL-USER> (mapcar #'+ '(1 2 3) '(5 6 7 8))(6 8 10) maplist:同mapcar...
阅读全文
摘要:它接受一个函数和一个参数列表,并返回把传入函数应用在传入参数的结果:CL-USER> (apply #'+ '(1 2 3))6CL-USER> (+ 1 2 3)6CL-USER> (funcall #'+ 1 2 3 4)10CL-USER> (apply #'+ '(1 2 3 4))10
阅读全文
摘要:我的理解:在一个函数或者某个语句块中可以定义或返回另一个函数,这个函数会依赖于包括它的函数或语句的某些变量,这个变量的传递就叫做闭包。正规定义:当函数引用到外部定义的变量时,这外部定义的变量称为自由变量(free variable)。函数引用到自由的词法变量时,称之为闭包(closure)。CL-USER> (defun add-to-list (num lst) (mapcar #'(lambda (x) (+ x num)) lst))ADD-TO-LISTCL-USER> (add-to-list 5 '(1 2 3))(6 7 8)CL-USER> (
阅读全文
摘要:(defun single? (lst) (and (consp lst) (null (cdr lst)))) CL-USER> (single? nil)NILCL-USER> (single? '(a))T (defun append1 (lst obj) (append lst (list obj))) CL-USER> (append1 '(a b c d) 'e)(A B C D ...
阅读全文
摘要:a.如果在一般形参前CL-USER> (defun hey (x &rest args) (member x args))HEYCL-USER> (hey '1 '(2 1 3))NILCL-USER> (hey '1 '2 '1 '3)(1 3)CL-USER> (member '1 '(2 1 3))(1 3)CL-USER> (defun world (&rest fn) fn)WORLDCL-USER> (world 'a 'b)(A B)CL-USER&
阅读全文
摘要:do: (do ((x a (b x)) (y c (d y))) ((test x y) (z x y)) (f x y)) 局部函数: (labels ((rec (x y) (cond ((test x y) (z x y)) (t (f x y) (rec (b x) (d y)))))) (rec a c)) 说明:上面代码中的b,d,test,z,f均为函数。
阅读全文
摘要:将下列表达式翻译成没有使用let与let*,并使用同样的表达式不被求值2次。 (a). (let ((x (car y))) (cons x x)) (b). (let* ((w (car x)) (y (+ w z))) (cons w y)) 答案: (a). ((lambda (x) (cons x x)) (car y)) (b). ((lambda (w) ((lambda (y) ...
阅读全文
摘要:(defconstant month #(0 31 59 90 120 151 181 212 243 273 304 334 365)) (defconstant yzero 2000) (defun leap? (y) (and (zerop (mod y 4)) (or (zerop (mod y 400)) (not (zerop (mod y 100)))))) (defun date-...
阅读全文
摘要:(defun leap? (y) (and (zerop (mod y 4)) (or (zerop (mod y 400)) (not (zerop (mod y 100)))))) 说明 闰年: 普通年:能被4整除 世纪年:能被400整除
阅读全文
摘要:4.7节的图4.4是这个样子,我认为改成这样才是对的,2和3应该换个位置。
阅读全文
摘要:(defun second-word (str) (let ((pl (+ (position #\ str) 1))) (subseq str pl (position #\ str :start pl))))
阅读全文
摘要:1 (defun mirror? (s)2 (let ((len (length s)))3 (if (evenp len)4 (do ((start 0 (+ start 1))5 (end (- len 1) (- end 1)))6 ((or (> start end)7 (not (equal (elt s start)8 (elt s end))))9 (> start end))))))相...
阅读全文
摘要:1 (defun compress (x) 2 (if (consp x) 3 (compr (car x) 1 (cdr x)) 4 x)) 5 6 (defun compr (elt n lst) 7 (if (null lst) 8 (list (n-elts elt n)) 9 (let ((next (car lst)))10 (if (equal next elt)11 (compr next (+ n 1) (cdr ...
阅读全文