Scheme-如何在';它不是由更多的列表组成的
系统将如下所示:Scheme-如何在';它不是由更多的列表组成的,scheme,racket,r5rs,Scheme,Racket,R5rs,系统将如下所示: (define (walk-list lst fun) ;;walk-list(list, fun) (if (not(null? lst)) ;;IF the list isn't NULL (begin (if (list? lst) ;;&& the list
(define (walk-list lst fun) ;;walk-list(list, fun)
(if (not(null? lst)) ;;IF the list isn't NULL
(begin
(if (list? lst) ;;&& the list is actually a list , THEN{
(begin
(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 ;;{
(walk-list (car lst) fun) ;;walk-list((car lst),fun)
(walk-list (cdr lst) fun))))))))) ;;walk-list((cdr lst),fun)
(walk-list test-document display) ;;walk through the list with the given document
(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
我试图让文档中的每个单词都有一个应用于它的函数。Where is says(有趣的列表)。但是函数永远不会被调用。首先
begin
用于执行多个表达式。第一个表达式需要有副作用,否则就是在浪费处理能力
即
如果没有多个表达式,则不需要使用begin
<代码>如果有3个部分。它们是:
(begin
(display "hello") ; display is a side effect
(something-else))
你应该正确识别你的代码。这是用DrRacket IDE标识的代码,删除了reduncantbegin
并添加了缺少的可选表达式,以便您看到它们返回的位置:
(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
那么(fun lst)
什么时候会被调用?从未!在((hel l o))((thhis)(is)(tes t))
和(equal?(car lst)())
中的任何汽车中都没有()
,如果(null)(car lst))
将始终是#f。因为我们知道(not(null?lst))
不是,所以它将行走汽车和cdr
,其中'undefined-return-2
或'undefined-return-3
将被评估,当所有内容都被访问并且没有任何处理时,过程停止
您还没有显示(walk list test document display)
应该显示的内容,但我粗略地猜测,除了pairs和null之外,您希望每个元素都显示它,因此我会这样写:
(define (walk-list lst fun) ;;walk-list(list, fun)
(if (not(null? lst)) ;;IF the list isn't NULL
(begin
(if (list? lst) ;;&& the list is actually a list , THEN{
(begin
(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 ;;{
(walk-list (car lst) fun) ;;walk-list((car lst),fun)
(walk-list (cdr lst) fun))))))))) ;;walk-list((cdr lst),fun)
(walk-list test-document display) ;;walk through the list with the given document
(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
累积树
您可以在中找到。它还展示了它的许多用途。为完整起见,我将在此处提供:
(accumulate-tree test-document display (lambda (a d) 'return) '())
从您的代码判断,您是学习第一个Lisp的Algol程序员。我建议您查看。是否可以显示对此过程的调用和预期的输出?函数已被调用,但您没有使用结果生成输出列表。函数将打印它。我要把它改成显示,它仍然没有显示任何东西。@PhilipRego你是什么意思<代码>(累积树测试文档显示(lambda(a d)‘return’)
打印“hellothistest”并(累积树测试文档(lambda(x)1)+0)
计算原子数。依我看,这不是什么。