英文:
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)
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论