2014-04-20 33 views
0
的列表中的每个列表
(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 
         )) 

我试图让每一个单词到文档有一个功能适用于它。在哪里说(有趣的名单)。但是这个功能从来没有被调用。

+0

您可以显示对此过程的调用和预期输出吗? – uselpa

+0

函数_is_被调用,但是你没有用结果构建输出列表 –

+0

该函数打印它。我要改变它来显示,它仍然不显示任何东西。 –

回答

1

首先。 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,所以它将步行carcdr,其中'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

+0

@PhilipRego你是什么意思? '(accumulate-tree test-document display(lambda(ad)'return)'())'打印出hellothisistest和'(accumulate-tree test-document(lambda(x)1)+ 0)',的原子。海事组织,这不是什么。 – Sylwester