[译] Lisp之根源(十六)

2019-05-19  本文已影响0人  日更专用小马甲
> (cadr '((a b) (c d) e))
(c d)

> (caddr '((a b) (c d) e)
e

> (cdar '((a b) (c d) e)
(b)

同样,我们用(list e1 ... en)代表(cons e1 ... (cons en '()) ... )

> (cons 'a (cons 'b (cons 'c '())))
(a b c)

> (lisp 'a 'b 'c)
(a b c)

现在,我们定义一些新的函数。我在函数名的末尾加了句号,以便于和原始的函数区分开。同时也防止和现有的Common Lisp的函数产生冲突。

  1. (null. x)判断参数是否是空列表。
(defun null. (x)
    (eq x '()))

> (null. 'a)
'()

> (null. '())
t
  1. (and. x y),当参数都为true时,返回true,否则返回false(即'())。
(defun and. (x y)
    (cond x (cond (y 't) ('t '())))
    ('t '())))

> (and. (atom 'a) (eq 'a 'a))
t

> (and. (atom 'a) (eq 'a 'b))
()
上一篇 下一篇

猜你喜欢

热点阅读