【问题标题】:Scheme - How do I get each list in a list that's not made up of more lists方案 - 如何在不由更多列表组成的列表中获取每个列表
【发布时间】:2014-04-20 17:44:02
【问题描述】:
   (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
                       ))

我试图让文档中的每个单词都应用一个函数。在哪里说(有趣的清单)。但该函数永远不会被调用。

【问题讨论】:

  • 你能显示对这个过程的调用和预期的输出吗?
  • 函数调用,但你没有用结果构建输出列表
  • 函数打印它。我要改成显示了,还是什么都不显示。

标签: scheme racket r5rs


【解决方案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 标识的代码,删除了冗余的 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) 应该显示的内容,但我猜测您希望它用于除对和 null 之外的每个元素,因此我会这样写:

(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 的 Algol 程序员。我建议你看看SICP videoes and book

【讨论】:

  • @PhilipRego 你是什么意思? (accumulate-tree test-document display (lambda (a d) 'return) '()) 打印“hellothisistest”,(accumulate-tree test-document (lambda (x) 1) + 0) 计算为原子数。 IMO 这不是什么。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-02-14
  • 1970-01-01
  • 2010-12-25
  • 1970-01-01
相关资源
最近更新 更多