Scheme - 如何将列表中的每个列表都放在一个不是由更多列表组成的列表中

时间:2014-04-20 17:44:02

标签: 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 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 test-document '(
                       ((h e l l o));;paragraph1
                       ((t h i s)(i s)(t e s t));;paragraph2
                       ))

我试图将每个单词放入文档中都应用了一个函数。在哪里说(有趣的名单)。但是这个函数永远不会被调用。

1 个答案:

答案 0 :(得分:1)

首先关闭。如果您需要执行多个表达式,则begin。然后第一个表达需要有副作用,否则它只是浪费处理能力。

(begin 
  (display "hello") ; display is a side effect
  (something-else))

如果您没有多个表达式begin,则不需要。{p> 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的代码,删除了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)什么时候被调用?决不! ()中的car (((h e l l o))((t h i s) (i s) (t e s t)))中只有(equal? (car lst) '())(null? (car lst)) (not (null? lst))中的car始终为#f。由于我们知道cdr是#t所以它会走'undefined-return-2'undefined-return-3,其中(walk-list test-document display)(accumulate-tree test-document display (lambda (a d) 'return) '()) 将被评估,并且当访问所有内容时过程停止没有处理。

你还没有显示accumulate-tree应该显示的内容但是我猜测你想要除了对和null之外的每个元素,因此我会这样写:

(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)))))
您可以在this SICP handout找到

{{1}}。它也证明了它的许多用途。为了完整起见,我将在此处提供:

{{1}}

从您的代码判断,您是Algol程序员,学习您的第一个Lisp。我建议你看一下SICP videoes and book