0

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?

Jason Aller
  • 3,541
  • 28
  • 38
  • 38
Netwave
  • 40,134
  • 6
  • 50
  • 93
  • Looks like homework. Remember that lists consist of pairs. Draw a picture of your list, its reverse and result you want as pairs - it should help. – hoha Nov 11 '12 at 12:55

2 Answers2

2

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.

Jean-Louis Giordano
  • 1,957
  • 16
  • 18
0

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

everbot
  • 13
  • 1
  • 3