Question

I'm making a function in scheme(lisp) in which I need to cons a list with it reverse, as below:

(cons list (cdr (reverse list)))

consider I have this list '(0 1 2), the desired output would be:

(0 1 2 1 0)

but I get:

((0 1 2) 1 0)

is there any function that returns the values of the list, not the list itself?

Était-ce utile?

La solution

You should read up on cons: http://en.wikipedia.org/wiki/Cons

cons will pair an element with another. If you pair an element with a list, you are adding this element at the beginning of the list. In your case, the element you are pairing to your reversed list is itself a list. See the examples on the wiki page.

What you want here is append, since you want to concatenate those two lists together.

Autres conseils

Append it like this:

(define (mirror ls)
    (if (null? (cdr ls))
        ls
        (let ((x (car ls)))
            (append 
                (list x)
                (mirror (cdr ls))
                (list x)))))

Hope it helps :)

Licencié sous: CC-BY-SA avec attribution
Non affilié à StackOverflow
scroll top