2

I'm just starting out with LISP, as in, just opened the book, I'm two pages into it. I'm trying to understand what is and what is not an acceptable fn call. Every time I try to execute

(1 2 3 4)

I get an illegal fn call error same goes for

(cdr (1 2 3 4))
(first (1 2 3 4))
(a b c d)

Are CL programs unable to return lists? How would I go about using these functions or printing a list? I'm using the SLIME implementation if it matters. LISP is very different than anything I've worked with before and I want to be sure I'm getting it conceptually.

3
  • The only slime I've heard of for lisp is the emacs development environment, which can interact with several different implementations. It's probably worth you working out what implementation you're using. Also, in case you haven't found it, google "Common Lisp Hyperspec". Commented Mar 9, 2011 at 7:49
  • 5
    I propose reading pages 3 and 4, too. Commented Mar 9, 2011 at 8:04
  • haha, nice. I've read past it considerably, but the book is branching off and assuming that I've been able to execute the above code. The book doesn't say anything about using ' though... Commented Mar 9, 2011 at 8:44

2 Answers 2

5

You need to quote lists if you are using them as constants. Otherwise, the system will try to call the function 1 on the arguments 2 3 4, which will not work (note that function calls have the same syntax as lists). Your examples should be:

'(1 2 3 4)
(cdr '(1 2 3 4))
(first '(1 2 3 4))
'(a b c d)
Sign up to request clarification or add additional context in comments.

2 Comments

or create a list explicitly, eg (list 1 2 3 4)
To clarify for the OP, the fourth example would need to be (list 'a 'b 'c 'd) when using the style @Nick D suggests.
-1

Hooo boy.

Look up Practical Common Lisp by Seibel. He's such a nice guy, he put it online for free reading. It's very useful.

Part of the definition of Lisp is this rule:

  • When a list is seen: Using the first element of the list, apply it to the rest of the list.

But wait: How do you actually enter lists then? There are two functions to do this: QUOTE and LIST.

As an example, let's print a list to the screen on standard out:

(format *standard-output* "~a" '(1 2 3 4))

For format, *standard-output* is aliased to t (well, at least in SBCL!), so usually we see (format t ....

5 Comments

@Rainer: Not technically. But as a first order approximation - it operates like one.
It does not. A function gets called with the arguments evaluated. QUOTE is a form that signals the evaluator that the enclosed form is evaluated to itself. It thus works totally different: it is not a function, it does not get called, it does take exactly one form and this form is returned as a value as it is.
@Rainer: Yet, (QUOTE (1 2 3 4)) <==> (1 2 3 4), which appears like the function QUOTE is called. Yes, it's a special form. I was trying to provide a simplified view for the OP without going into the differences between functions, macros, and special forms.
It does not appear. If there would be a function QUOTE called, the argument would have been evaluated and an error had happened.
QUOTE is similar to a function call in that both are lisp forms. They are quite different in how they are evaluated (QUOTE is a special form, while a function call is ... a function call).

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.