lisp 小代码1
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
页:
[1]