Lisp HUG Maillist Archive

is it because they are functions?

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.

Re: is it because they are functions?

On Feb 9, 2010, at 8:42 AM, Aleksandar Matijaca wrote:

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

1. Lisp functions receive all their arguments already evaluated. What is the evaluation of (1 2 3 4)?
hint: the number 1 is not a function. When lispworks, or any other common lisp, tries to
evaluate (reverse (1 2 3 4)) it first must evaluate (1 2 3 4) - not (list 1 2 3 4), nor (quote 1 2 3 4)
which is what '(1 2 3 4) is a shorthand for. Since the number 1 is not a function, you get
and error, specifically "Illegal argument in functor position: 1 in (1 2 3 4)." LispWorks is even
nice enough to tell you what exactly in your input is *not* a function, here, the number 1.

2. You definitely need to read, thoroughly and completely, an introduction to lisp. This
mailing list is intended for questions specific to LispWorks Common Lisp, not general newbie
questions about the language. For that, try reading an introductory text, or the usenet
group comp.lang.lisp.

For a good, free, online intro, try:
<http://www.gigamonkeys.com/book/>

warmest regards,

Ralph


Raffael Cavallaro
raffaelcavallaro@me.com






Re: is it because they are functions?

Alex - Every S-expression will be evaluated if not quoted. So (1 2 3 4) will be evaluated, and the atom in the first position - 1 in this case - is expected by the reader to be a function or macro. '(1 2 3 4) is shorthand for (QUOTE 1 2 3 4), which returns the list composed of 1,2,3,and 4. That list can then be acted upon by REVERSE in this case, whereas without QUOTE, (1 2 3 4) will be evaluated, and if the first atom isn't a function or macro, the REPL will complain.


Aleksandar Matijaca (02/09/2010 05:42 AM) 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.

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/


Re: is it because they are functions?

Am 09.02.2010 um 14:42 schrieb Aleksandar Matijaca:

> 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.
> 


Here is another book introducing some basic Common Lisp:

   http://www.cs.cmu.edu/~dst/LispBook/

You can download the PDF.

Practical Common Lisp, by Peter Seibel, is a bit more advanced and also more practical.

While reading these books it is very useful to try out the examples and exercises directly with the LispWorks editor and listener.

Regards,

Rainer Joswig

Rainer Joswig, Hamburg, Germany
http://lispm.dyndns.org/
mailto:joswig@lisp.de




Updated at: 2020-12-10 08:39 UTC