Re: is it because they are functions?
On Tue, Feb 9, 2010 at 8:42 AM, Aleksandar Matijaca <amatijaca@gmail.com> wrote:
> Hi there,
> I have a question --
> if I write in REPL
> (reverse '(1 2 3 4))
> I get a reasonable result -- that is, reverse reverses a quoted list in this
> case...
> Why is it then that this fails
> (reverse (1 2 3 4))
> Can't reverse deal with a non-quoted list? Or is it that functions can only
> deal
> with quoted "things" (sorry, I am missing here the exact English language
> expression
> as to how to express myself)...
> Thanks, Alex.
HI Alex,
This is a more fundamental question about Lisp, and not LispWorks
specific. As such, you might be better off finding an introductory
book about Lisp. Some popular ones include Practical Common Lisp [1],
On Lisp [2], ANSI Common Lisp [3]. The first two of these are freely
available online, and there are lots of other resources out there too.
Now, toward your original question… As you saw, (reverse '(1 2 3 4))
produces (4 3 2 1). What do you expect (reverse (reverse '(1 2 3 4)))
to return? I hope that you expect (1 2 3 4) and not ((1 2 3 4)
reverse). Reverse accepts a single list l and returns a new list
containing the elements of l in reverse order. When (reverse <form>)
appears in a program, the <form> is evaluated to produce a value,
which should be a list, and then this value is passed to reverse.
Whether the <form> is a quoted list or something else doesn't matter,
so long as it produces a list. The reason that (reverse (1 2 3 4))
does work, is that here <form> is (1 2 3 4), and to evaluate (1 2 3 4)
means to find the function associated with 1 (and there isn't one) to
evaluate 2, 3, and 4, to produce the values 2, 3, and 4, and then to
call the function associated with 1 with the values 2, 3, and 4.
Since there is no such function, this will fail. If you've tried this
in the REPL, you'll see something like:
CL-USER 47 > (1 2 3 4)
Error: Illegal argument in functor position: 1 in (1 2 3 4). …
(reverse '(1 2 3 4)) works because the value of '(1 2 3 4) is a list.
The value of (list 1 2 3 4) is also a list, so (reverse (list 1 2 3
4)) will work for you too. Similarly with (reverse (reverse '(1 2 3
4))) and (reverse (reverse (list 1 2 3 4))).
But I think you need to find some introductory Lisp material before
going much farther.
Cheers,
//JT
[1] http://www.gigamonkeys.com/book/
[2] http://www.paulgraham.com/onlisp.html
[3] http://www.paulgraham.com/acl.html
--
Joshua Taylor, http://www.cs.rpi.edu/~tayloj/