(defun highest (lat)
(cond
((null lat) nil)
((null (cdr lat)) (car lat))
(T (higher (car lat) (highest (cdr lat))))))
(defun higher (a1 a2)
(cond
((> a1 a2) a1)
(T a2)))
此功能按预期工作:
> (highest '(3 5 1 2 3))
3. Trace: (HIGHEST '(3 5 1 2 3))
4. Trace: (HIGHEST '(5 1 2 3))
5. Trace: (HIGHEST '(1 2 3))
6. Trace: (HIGHEST '(2 3))
7. Trace: (HIGHEST '(3))
7. Trace: HIGHEST ==> 3
6. Trace: HIGHEST ==> 3
5. Trace: HIGHEST ==> 3
4. Trace: HIGHEST ==> 5
3. Trace: HIGHEST ==> 5
但是,如果我将参数更改为&rest
:
(defun highest (&rest args)
(cond
((null args) nil)
((null (cdr args)) (car args))
(T (higher (car args) (highest (cdr args))))))
它的行为不一样。
> (highest 3 5 1 2 3)
3. Trace: (HIGHEST '3 '5 '1 '2 '3)
4. Trace: (HIGHEST '(5 1 2 3))
4. Trace: HIGHEST ==> (5 1 2 3)
*** - >: (5 1 2 3) is not a real number
编辑:对不起,我忘了提到在第二种情况下我将参数传递给原子。我编辑了问题以使其更清楚。