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

ad absurdum
  • 15,925
  • 4
  • 28
  • 49
DarKing
  • 13
  • 2
  • 1
    '_but it doesn't create the list the way I want_' is not very helpful. What's the current result? What's the expected/required result? – ack Dec 21 '20 at 23:30
  • (evenList (list 1 2 3 4)) The answer to this function in the above codes (cons 2 (cons 4 '())) but I want it to be like this (list 2 4) – DarKing Dec 22 '20 at 06:59
  • Thank you mate it solved my problem – DarKing Dec 22 '20 at 15:52
  • @DarKing -- I edited your question to clarify the problem and make it easier to find for future visitors; I hope that this is ok. I also moved my earlier comment into an answer. – ad absurdum Dec 22 '20 at 16:07

1 Answers1

1

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.

ad absurdum
  • 15,925
  • 4
  • 28
  • 49
  • 1
    we really should make some specific tag for this "print-formatting" thing in Racket. these questions keep comping up, and having the tag would help with finding similar questions. – Will Ness Dec 22 '20 at 23:14
  • @WillNess -- that is a good idea. I looked for a dupe before I answered, and couldn't find anything; a tag would be helpful. Since printed representations are so important in lisps in general, I might nominate [tag:printed-representation]. At a glance, I don't see anything like it available right now. – ad absurdum Dec 23 '20 at 01:21
  • good idea, I think. sure will get picked up by other languages as well, with rich surface syntax for data, like e.g. (I hear;)) Clojure. you should just go ahead and add this tag here on this Q. if for some reason you're not allowed to, ping me and I'll do it, I did it before too (with e.g. [tag:map-function]). but since you've come up with it, you should have the honors of doing it yourself. :) – Will Ness Dec 23 '20 at 07:06
  • how about we rename it as "repl-printed-representation", to be more specific? I've found and tagged only few questions so far, it's not a bother to go over and retag them. another option is to always also tag them as "repl", but the 5 tags limit sometimes doesn't allow that. what do you think? "printed-representation" seems too broad, could also include serialization issues, or be mistaken as such and get removed. (!) – Will Ness Dec 23 '20 at 08:25
  • @WillNess -- I'm not necessarily opposed to that, but I don't think of printed representations as just for the REPL. Lisp readers read printed representations, and can read them from files for example. I'm not sure that there is a gain by being more specific, but the REPL is also probably where most issues would appear for askers. I don't feel too strongly either way.... – ad absurdum Dec 23 '20 at 08:32
  • Yeah, I was typing when you added that; it is a fair point. OK, agreed. – ad absurdum Dec 23 '20 at 08:34
  • great. now let's see if we get scorned by the meta crowd. :) :) – Will Ness Dec 23 '20 at 08:39
  • fingers crossed ;) – ad absurdum Dec 23 '20 at 08:39