简体   繁体   English

反转方案语言中的列表。 错误:汽车:违反合同预期:对? 给定:一个

[英]Invert list in Scheme Language. Mistake: car: contract violation expected: pair? given: a

I practice in Scheme language.我用 Scheme 语言练习。 My code is below Scheme the procedure called (invert lst) that reverses the list lst (any sub-lists of lst should be reversed as well).我的代码在Scheme下面,称为(invert lst)的过程反转列表lst(lst的任何子列表也应该反转)。 I initially went down this path, but I'm having an inconsistent reversal (inconsistent) of the list in the last example.我最初是沿着这条路走的,但是在最后一个示例中,我的列表反转(不一致)不一致。

My code:我的代码:

#lang scheme

(require "project3test.scm") 
(define (invert lst)
  (if (null? lst)
      '()
      (if (not (list? lst))
          lst
          (append (invert (cdr lst))
                  (list (car lst))))))

(test-invert invert)

Test file "project3test.scm":测试文件“project3test.scm”:

#lang scheme

(provide test-invert)

(define test-invert
  (lambda (x)
    (begin
      (newline)
      (write "Question 5: invert")
      (newline)
      (write (x 'a))(newline)
      (write (x '(1 2 3)))(newline)
      (write (x '(1 2 (3 4) (5 (6 7 (8))) 9)))(newline)
      )))

Output:输出:

"Question 5: invert"
a
(3 2 1)
(9 (5 (6 7 (8))) (3 4) 2 1)

The problem is here.问题就在这里。 The numbers are not consecutive, not fully implemented.这些数字不是连续的,没有完全执行。 What is my mistake?我的错误是什么?

Then I tried another way, but the following error occurs: car: contract violation expected: pair? given: a然后我尝试了另一种方法,但是出现以下错误: car: contract violation expected: pair? given: a car: contract violation expected: pair? given: a

#lang scheme

(require "project3test.scm")

(define (inverse ls)
  (cond ((null? ls)
         ls)
        ((list? (car ls))
         (append (inverse (cdr ls))
                 (list (inverse (car ls)))))
        (else
         (append (inverse (cdr ls))
                 (list (car ls))))))

(test-invert invert)

If I run this code separately from the test file, with the output (display ....) it works and turns the sheet over correctly.如果我将此代码与测试文件分开运行,输出(显示 ....)它可以正常工作并正确翻转工作表。

#lang scheme

(define (inverse ls)
  (cond ((null? ls)
         ls)
        ((list? (car ls))
         (append (inverse (cdr ls))
                 (list (inverse (car ls)))))
        (else
         (append (inverse (cdr ls))
                 (list (car ls))))))

(display (inverse '(1 2 (3 4) (5 (6 7 (8))) 9)))

Output:输出:

  (9 (((8) 7 6) 5) (4 3) 2 1)

I would be grateful for any advice, any hint, and help.我将不胜感激任何建议、任何提示和帮助。

Your error is caused by one of the test cases: (write (x 'a))(newline) .您的错误是由以下测试用例之一引起的: (write (x 'a))(newline) a isn't list, so it can't be reversed. a不是列表,因此不能反转。

When you rewrite this test case and use your inverse function, everything seems to work:当您重写此测试用例并使用您的inverse函数时,一切似乎都正常:

#lang scheme

(define (test-invert fn)
  (writeln "Question 5: invert")
  (writeln (fn '(a)))
  (writeln (fn '(1 2 3)))
  (writeln (fn '(1 2 (3 4) (5 (6 7 (8))) 9))))

(define (inverse ls)
  (cond ((null? ls) ls)
        ((list? (car ls)) (append (inverse (cdr ls))
                                  (list (inverse (car ls)))))
        (else (append (inverse (cdr ls))
                      (list (car ls))))))

(test-invert inverse)

Output:输出:

"Question 5: invert"
(a)
(3 2 1)
(9 (((8) 7 6) 5) (4 3) 2 1)

Note that there's a function reverse , which doesn't reverse nested lists, but can be used to write a function which does so.请注意,有一个函数reverse ,它不会反转嵌套列表,但可用于编写一个这样做的函数。

(define (deep-reverse o)
  (if (list? o)
      (reverse (map deep-reverse o))
      o))

(test-invert deep-reverse)

There are 4 limit cases:有4种极限情况:

  • null list空列表
  • car is a list汽车是一个清单
  • car is not a list汽车不是清单
  • bottom in case of non-list input非列表输入的底部

Here is the code这是代码

(define inv
  (lambda (l)
    ((lambda (s) (s s '() l))
     (lambda (s a l)
       (if (null? l)
           a
           (s s
              (cons
               (if (pair? (car l))
                   (inv (car l))
                   (car l))
               a)
              (cdr l)))))))

If you want to write the same code in a compressed form, you can do so:如果您想以压缩形式编写相同的代码,可以这样做:

(define inv
  (lambda (l)
    (fold-left (lambda (a x)
                 (if (pair? x)
                     (cons (inv x) a)
                     (cons x a)))
               '()
               l)))

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM