[英]Why does the list result returned by my function look funny?
(define (evenList xs)
(cond
((null? xs) '())
((eq? (cdr xs) '()) '())
(else (cons (cadr xs) (evenList (cddr xs))))))
I'm using this code but it doesn't create the list the way I want it.我正在使用此代码,但它不会按照我想要的方式创建列表。
(evenList (list 1 2 3 4))
evaluates to (cons 2 (cons 4 '()))
in the REPL, but I want it to be like (list 2 4)
. (evenList (list 1 2 3 4))
(cons 2 (cons 4 '()))
,但我希望它像(list 2 4)
。
Your code works and gives the correct output as far as I can tell.据我所知,您的代码有效并给出了正确的 output 。 I'm guessing that you are using the Beginning Student Language.
我猜你正在使用初级学生语言。 The list
(2 4)
is represented as (cons 2 (cons 4 '()))
in the REPL when using the Beginning Student Language;使用初级学生语言时,列表
(2 4)
在 REPL 中表示为(cons 2 (cons 4 '()))
; this same list is represented as (list 2 4)
in the REPL when using the Intermediate Student Language.当使用中级学生语言时,这个相同的列表在 REPL 中表示为
(list 2 4)
。 In #lang racket
you would see this represented as '(2 4)
in the REPL.在
#lang racket
,您会在 REPL 中看到这表示为'(2 4)
。 In all cases the underlying list data structure is the same;在所有情况下,底层列表数据结构都是相同的; this is just a matter of the printed representation of the list.
这只是列表的打印表示的问题。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.