From: Young-Jin Lee
Subject: [Q] lisp newbie's question...
Date: 
Message-ID: <77en7.1343$rT2.17474@vixen.cso.uiuc.edu>
Hi, I'm learning Lisp and I have couple of questions.

What are the arguments starting with ":" in the following code segment?

(in-package :COMMON-LISP-USER)

(defstruct (problem (:PRINT-FUNCTION problem-print-procedure)
      (:CONC-NAME pr-))
      ....
)

I think I knew the basic LISP commands and I wrote several basic examples
with LISP, but I don't know what they (argument starting with ":"). Is there
any on-line document explaining these things?

Thanks in advance.

Young-Jin

From: Frederic Bastenaire
Subject: Re: [Q] lisp newbie's question...
Date: 
Message-ID: <d977c973.0109102336.2e9b7174@posting.google.com>
> 
> What are the arguments starting with ":" in the following code segment?
> 
The symbols starting with a column ":" are called keywords; they belong to the
KEYWORD package (whose name is implicit, they could be written KEYWORD:XXX).
Thay are often used to name arguments, options...

A keyword symbol evaluates to itself (no need to quote them).

Functions can have keywords to name arguments:
 define them with (defun foo (&key a1 a2 a3) ....)
 then you call them like that (foo :a2 <value-of-a1> :a1 <value-of-a1> ...)
this way, the order of argument does not matter; any non supplied argument
takes its default value.
It is a bit more verbose than usual arguments, but it is really useful if
a function has many optional arguments.

Keywords have many other uses, e.g. they can be used as ENUM types in C, just
like all symbols, but you are sure that there is no package issue (which is
sometimes tricky...).

Yours,

FB
From: Richard Krush
Subject: Re: [Q] lisp newbie's question...
Date: 
Message-ID: <9nk0l1$7fqns$1@ID-60069.news.dfncis.de>
Young-Jin Lee <······@uiuc.edu> wrote:
> Hi, I'm learning Lisp and I have couple of questions.
> What are the arguments starting with ":" in the following code segment?

> (in-package :COMMON-LISP-USER)

> (defstruct (problem (:PRINT-FUNCTION problem-print-procedure)
>      (:CONC-NAME pr-))
>      ....
> )

> I think I knew the basic LISP commands and I wrote several basic examples
> with LISP, but I don't know what they (argument starting with ":"). Is there
> any on-line document explaining these things?

Well, those arguments with : are called keywords. You can learn about
keywords from most Lisp tutorials or books, and of course from the CLtL2
or CL HyperSpec.

Here are some useful URLs:
http://www.supelec.fr/docs/cltl/clm/node282.html#SECTION003216500000000000000
http://psg.com/~dlamkins/sl/chapter21.html


Hope this helps!

-- 
 Richard Krushelnitskiy   "I know not with what weapons World War III will
 ·········@gmx.net         be fought, but World War IV will be fought with
 http://rkrush.cjb.net     sticks and stones." -- Albert Einstein
From: Kaz Kylheku
Subject: Re: [Q] lisp newbie's question...
Date: 
Message-ID: <13gn7.159513$B37.3576778@news1.rdc1.bc.home.com>
In article <····················@vixen.cso.uiuc.edu>, Young-Jin Lee wrote:
>Hi, I'm learning Lisp and I have couple of questions.
>
>What are the arguments starting with ":" in the following code segment?
>
>(in-package :COMMON-LISP-USER)
>
>(defstruct (problem (:PRINT-FUNCTION problem-print-procedure)
>      (:CONC-NAME pr-))
>      ....
>)

The leading colon designates symbols that are interned in the keyword
package, that's all! :FOO is a shorthand for KEYWORD:FOO. You can
test this using the expression

	(eq ':foo 'keyword:foo)

which should yield T.

Unlike other symbols, symbols in the keyword package have a special
property: they evaluate to themselves. So you often see them used without
quoting. The above expression could be written

	(eq :foo keyword:foo)

No error message like ``variable :foo is not bound to any value'' appears;
the value of :foo is just :foo.

Keywords are used in situations when you need to invent your own
identifiers for whatever purpose. They are often used to denote the
keyword arguments of a lambda list. Any symbols could be used, but it's
customary to use symbols from the keyword package. 

Keywords simply stand for themselves; they cannot be bound to a value. As
such, they are useful as syntactic markers.

Many standard Lisp macros and functions support keyword arguments in their
syntax, so you are going to come across them frequently.

Here is how to make your own function that uses keywords to specify
keyword parameters.

	(defun coffee (type &key sugar cream)
	 (format t "coffee type: ~A~%" type)
	 (if sugar
	  (format t "number of sugar cubes: ~A~%" sugar)
	  (format t "no sugar~%"))
	 (if cream
	  (format t "number of creams: ~A~%" cream)
	  (format t "no cream~%")))

	(coffee :dark-roast :cream 2)

The function has one required parameter ``type'', and two keyword
parameters. You can call it like this:

	(coffee :dark-roast)

in this call, the type parameter has the value :black, and the sugar
and cream parameters have the value NIL.  You can call it like
this also:

	(coffee :dark-roast :cream 2)

Now the cream parameter has value 2. Note how a keyword symbol is used to
specify the parameter in the call, but an ordinary symbol receives the
value within the function. And of course if you like your coffee sweet,
you can have:

	(coffee :dark-roast :cream 2 :sugar 2)