kernan373
14th June 2012, 10:16 AM
Hey all,
I'm trying to write a function in Racket to reverse a list, including the lists within the list. The input should be one list and the output should be the reversed list.
I made a function that reverse a list's elements without going into the lists within the lists.
so
(simple-reverse (list 1 2 (list 'a 'b 'c) 3 4))
would return:
(list 4 3 (list 'a 'b 'c) 2 1)
Here's what I have so far of the function I want to make:
(define reverse-function
(lambda (L)
(cond
((empty? L) empty)
((list? (first L)) (cons (reverse-function (first L)) (reverse-function (rest L))))
(else (cons (first L) (reverse-function (simple-reverse (rest L)))))))
The output expected from input (list 1 2 3 (list 'a 'b 'c) 4 5) should be (list 5 4 (list 'c 'b 'a) 3 2 1). How do I go about doing this? what am I doing wrong?
I'm trying to write a function in Racket to reverse a list, including the lists within the list. The input should be one list and the output should be the reversed list.
I made a function that reverse a list's elements without going into the lists within the lists.
so
(simple-reverse (list 1 2 (list 'a 'b 'c) 3 4))
would return:
(list 4 3 (list 'a 'b 'c) 2 1)
Here's what I have so far of the function I want to make:
(define reverse-function
(lambda (L)
(cond
((empty? L) empty)
((list? (first L)) (cons (reverse-function (first L)) (reverse-function (rest L))))
(else (cons (first L) (reverse-function (simple-reverse (rest L)))))))
The output expected from input (list 1 2 3 (list 'a 'b 'c) 4 5) should be (list 5 4 (list 'c 'b 'a) 3 2 1). How do I go about doing this? what am I doing wrong?