首先。 begin
是如果你需要做多个表达式。第一个表达式需要有副作用,否则这只是浪费处理能力。
即,
(begin
(display "hello") ; display is a side effect
(something-else))
当您没有多个表达式begin
是不需要的。 if
有3个部分。它们是:
(if predicate-expression ; turnas into something true or #f (the only false value)
consequent-expression ; when predicate-expression evalautes to anything but #f
alternative-expression) ; when predicate-expression evaluates to #f this is done
您应该正确识别您的代码。这里是DrRacket IDE idented代码,与reduncant begin
删除和添加缺失的另类表达,所以你看到他们返回:
(define (walk-list lst fun) ;;walk-list(list, fun)
(if (not (null? lst)) ;;IF the list isn't NULL
(if (list? lst) ;; && the list is actually a list , THEN{
(if (equal? (car lst) '()) ;; IF the first element in the list is empty
(fun lst) ;; THEN call the function on the list (funct is supose to get each word)
(if (not (null? lst)) ;; ELSE IF the first item isn't a list
(begin ;; Here begin is needed
(walk-list (car lst) fun) ;; walk-list((car lst),fun)
(walk-list (cdr lst) fun)) ;; walk-list((cdr lst),fun)
'undfined-return-1)) ;; stop recursion, return undefined value
'undefined-return-2) ;; stop recursion, return undefined value
'undefined-return-3)) ;; stop recursion, return undefined value
所以当不(fun lst)
被调用?决不!在(((h e l l o))((t h i s) (i s) (t e s t)))
和(equal? (car lst) '())
的car
中没有()
,这是(null? (car lst))
将始终是#f。由于我们知道(not (null? lst))
为#t,所以它将步行car
和cdr
,其中'undefined-return-2
或'undefined-return-3
将被评估,并且过程在所有内容都被访问并且没有处理时停止。
您还没有表现出应该显示哪些(walk-list test-document display)
,但我让你想它只是对和空的每一个元素胡乱猜测,这样我就写了这是这样的:
(accumulate-tree test-document display (lambda (a d) 'return) '())
accumulate-tree
你将在this SICP handout中找到。它也演示了它的许多用途。为求完整,我会在这里为它供给:
(define (accumulate-tree tree term combiner null-value)
(cond ((null? tree) null-value)
((not (pair? tree)) (term tree))
(else (combiner
(accumulate-tree (car tree)
term
combiner
null-value)
(accumulate-tree (cdr tree)
term
combiner
null-value)))))
从你看代码,你是一个大陵五程序员学习你的第一个Lisp的。我建议你看看SICP videoes and book。
您可以显示对此过程的调用和预期输出吗? – uselpa
函数_is_被调用,但是你没有用结果构建输出列表 –
该函数打印它。我要改变它来显示,它仍然不显示任何东西。 –