racketinterpreterread-eval-print-loopracket-student-languagesrepl-printed-representation

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).


Solution

  • Your code works and gives the correct output as far as I can tell. 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; this same list is represented as (list 2 4) in the REPL when using the Intermediate Student Language. In #lang racket you would see this represented as '(2 4) in the REPL. In all cases the underlying list data structure is the same; this is just a matter of the printed representation of the list.