简体   繁体   English

为什么我的 function 返回的列表结果看起来很有趣?

[英]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.

相关问题 为什么我的函数有时会返回 (shared ...) 而不是 (list ...) - Why does my function sometimes return (shared …) instead of (list …) 为什么我的函数不断返回一个空列表? - Why does my function keep returning an empty list? 带有 Y 组合器的列表函数没有递归,为什么? - List function with Y combinator does no recursion, why? 为什么我的列表反向函数只能自己工作,而不是另一个函数内部工作? - Why is my list reverse function working on its own, but not inside of another function? 为什么这会返回列表'(5)而不是数字5? - Why does this return a list '(5) rather than the number 5? 使用我自己的函数对列表进行排序 - Sorting a list using my own function to sort 在球拍中,我创建了一个函数来查找列表中的最大值,但是我的一些测试用例无法正常工作,为什么其中一些无法正常工作? - In racket I have created a function to find the maximum value in a list but some of my test cases aren't working, why aren't some of them working? 为什么这个球拍功能需要参数? 它是如何工作的? - Why does this racket function need an argument? And how does it work? 如何在球拍列表中查找元素 - How to look for an element in a list of lists in racket 为什么我的结果中会出现“list”这个词? - Why is the word "list" showing up in my results?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM