clisp小代码

11 views
Skip to first unread message

linlin lin

unread,
Jun 8, 2011, 11:28:46 PM6/8/11
to lisp-...@googlegroups.com
计划学习common lisp,希望可以与大家一起进步。

写了几段小代码,同时贴到了eyeit ,初学,水平有限,高手莫笑。

(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)) 

主要是实现加减乘除。

潘飞

unread,
Jun 8, 2011, 11:34:03 PM6/8/11
to lisp-...@googlegroups.com
我也是初学者,希望能有所精进,在读 practical common lisp .

2011/6/9 linlin lin <maka...@gmail.com>



--
Stay Hungry. Stay Foolish.

linlin lin

unread,
Jun 9, 2011, 12:06:46 AM6/9/11
to lisp-...@googlegroups.com
我在读 ansi common lisp (第2版) 和 onlisp 。
Reply all
Reply to author
Forward
0 new messages