common lisp是一种强大的语言。
日后在这里贴一些书上摘录或是自己写的一些小段代码.
(defun double (x)
(+ x x ))
(defun ^ (&rest lst)
(let ((sum (car lst)))
(dolist (i (cdr lst))
(setf sum (expt sum i)))
sum))
(defun for-operation (fn &rest lst)
(let ((lst (car lst)))
(let ((sum (car lst)))
(if (null lst) (setq sum 0))
(dolist (i (cdr lst))
(setq sum (funcall fn sum i)))
sum)))
(defun my-plus (&rest lst)
(for-operation #'+ lst))
(defun my-mil (&rest lst)
(for-operation #'* lst))
(defun my-sub (&rest lst)
(for-operation #'- lst))
(defun my-div (&rest lst)
(for-operation #'/ lst))
(defun odd-obj (lst)
(if (consp lst)
(cons (car lst) (odd-obj (cdr (cdr lst))))
nil))
(defun even-obj (lst)
(if (consp (cdr lst))
(cons (car (cdr lst)) (even-obj (cdr (cdr lst))))
nil))
(setq double 4)
(print (double 3)) ;6
(print (double double)) ;8
(print (symbol-value 'double)) ;4
(print #'double)
(print (symbol-function 'double))
(print (odd-obj '(a b c d e f g h i j ))) ; A C E G I
(print (even-obj '(a b c d e f g h i j ))) ; B D F H J
(print (^ 2 3 4)) ; 4096 ( (2^3)^4 ; 2^3 == (expt 2 3) equal 8)
(print (my-plus 1 2 3 4 5 6))
(print (my-sub 1 2 3 4 5 6))
(print (my-mil 1 2 3 4 5 6))
(print (my-div 1 2 3 4 5 6))
; above write at 2011/6/8 23:16
本文提供了Common Lisp语言中几个实用函数的定义与使用示例,包括加减乘除运算符的自定义实现、幂运算、奇数位和偶数位元素提取等。通过这些示例代码,读者可以更好地理解Common Lisp的高级特性。
1890

被折叠的 条评论
为什么被折叠?



