문제

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?

도움이 되었습니까?

해결책

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.

다른 팁

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

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top