使用lisp在cond中输出内容

时间:2012-07-24 01:19:26

标签: lisp scheme

(define length1
 (lambda (lat)
  (cond 
   ((null? lat) 0)
   (else (+ 1 (length1 (cdr lat)))))))

例如:在cond

中调用length1时显示数字(或其他任何内容)

1 个答案:

答案 0 :(得分:1)

对于常见的lisp,您可以使用(progn (...) (...) ...)将多个表达式组合成一个。

方案中的等价物是(begin (...) (...) ...)

这样:

(define length1
 (lambda (lat)
  (cond 
   ((null? lat) 0)
   (else (begin (display "hello world") (+ 1 (length1 (cdr lat))))))))

或者你想要:

(define length1
 (lambda (lat)
  (cond 
   ((null? lat) 0)
   (else (let ((or-anything-else (+ 1 (length1 (cdr lat)))))
            (display or-anything-else)
            or-anything-else)))

这就是我的耐心耗尽。