hedgehogrider
hedgehogrider

Reputation: 1198

Using lists with Common LISP

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.

Upvotes: 2

Views: 486

Answers (2)

Paul Nathan
Paul Nathan

Reputation: 40299

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

Upvotes: -1

Jeremiah Willcock
Jeremiah Willcock

Reputation: 30969

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)

Upvotes: 5

Related Questions