Racket:合同违规,期望 (and/c list? (not/c empty?)) 给定:'()?

huangapple go评论61阅读模式
英文:

Racket: Contract violation, (and/c list? (not/c empty?)) expected given: '()?

问题

我在网上搜索了很长时间,但没有用。请帮助或尝试提供一些如何实现这个目标的想法。

我写的是函数首先接受一个参数,一个列表,它要么是一个空列表,要么只包含非空列表。它构建另一个列表,由每个内部列表的第一个S表达式组成。
我的代码如下:

(define (first l)
    (cond
        [(null? l) l]
        [(null? (car l)) (cons '() (first (cdr l)))]
    )
)

但是当我调用(first '(()))时,出现了这个错误。我对此毫无头绪。提前感谢。

想知道是否这样做是一个好的做法,以及最佳的做法是什么?

英文:

I am searching for a long(长) time on net. But no use. Please help or try to give some ideas how to achieve this.

what i am write is The function firsts takes one argument, a list, which is either a null list or contains only non-empty lists. It builds another list composed of the first S-expression of each
internal list.
and my code is as follows:

(define (first l)
    (cond
        [(null? l) l]
        [(null? (car l)) (cons '() (first (cdr l)))]
    )
)

but when i call (first '(())),it goes out this error.i hava no idea about this.Thanks in advance.

Wanted to know if it's good practice to do that and what would(将) be the best way to do that?

答案1

得分: 1

If you intend to make the call (first '(())), then your function should not assume that all elements of the input list are nonempty lists. In this case, when traversing the input list, your function must ignore those elements for which function (car lst) is not defined. Also, since (first lst) is a predefined function in Racket, it would be better to use another name for your function, for example cars.

(define (cars lst)
  (cond [(empty? lst) lst]
        [(pair? (car lst)) (cons (caar lst) (cars (cdr lst)))] ; process first element
        [else (cars (cdr lst))]))                              ; ignore first element

Note that (pair? (car lst)) is used to filter relevant elements of the input list, and (caar lst), that is equivalent to (car (car lst)), is used to get the first element of the first list of lst.

Examples:

> (cars '((a b) (c) (d e f))) 
'(a c d)

> (cars '((a b c) () (d e f)))
'(a d)

> (cars '((a) (b c) d (e f)))
'(a b e)

REMARK If the input list really only contains nonempty lists, you can get the desired result using function map, as follows:

> (map car '((a b) (c) (d e f) (g h))) 
'(a c d g)
英文:

If you intend to make the call (first '(())), then your function should not assume that all elements of the input list are nonempty lists. In this case, when traversing the input list, your function must ignore those elements for which function (car lst) is not defined. Also, since (first lst) is a predefined function in Racket, it would be better to use another name for your function, for example cars.

(define (cars lst)
  (cond [(empty? lst) lst]
        [(pair? (car lst)) (cons (caar lst) (cars (cdr lst)))] ; process first element
        [else (cars (cdr lst))]))                              ; ignore first element

Note that (pair? (car lst)) is used to filter relevant elements of the input list, and (caar lst), that is equivalent to (car (car lst)), is used to get the first element of the first list of lst.

Examples:

> (cars '((a b) (c) (d e f))) 
'(a c d)

> (cars '((a b c) () (d e f)))
'(a d)

> (cars '((a) (b c) d (e f)))
'(a b e)

REMARK If the input list really only contains nonempty lists, you can get the desired result using function map, as follows:

> (map car '((a b) (c) (d e f) (g h))) 
'(a c d g)

huangapple
  • 本文由 发表于 2023年6月12日 10:37:17
  • 转载请务必保留本文链接:https://go.coder-hub.com/76453341.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定