ascending

Discussion of Common Lisp
Post Reply
murali
Posts: 15
Joined: Fri Jul 01, 2011 10:11 am

ascending

Post by murali » Fri Jul 01, 2011 10:35 am

2. Write a function called reverse, of one argument which
is a list, which returns the items in the list in reverse order,
For example,
(reverse '(a b d))
should return: (d b a)

I X Code X 1
Posts: 59
Joined: Sun May 29, 2011 8:52 pm
Location: NY
Contact:

Re: ascending

Post by I X Code X 1 » Fri Jul 01, 2011 12:06 pm

I know he didn't post any code, but I wanted practice so I wrote it anyway.

Code: Select all

(defun my-reverse (list)
  (let ((x nil))
    (loop for i in list do
          (push i x)
          finally (return x))))

Code: Select all

COMMON-LISP-USER>
(my-reverse '(a b d))

(D B A)

Note: To be a true reverse function it will need to work with strings, "hello", and vectors/arrays, #(1 2 3 4); mine does not. You can try to figure that out on your own.

Post Reply