Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

A list of integers...

22 views
Skip to first unread message

Travish Robinson

unread,
Jul 19, 1997, 3:00:00 AM7/19/97
to

I'm just starting out using lisp and I need some help. I need to write
a function that could take an integer as an arg. and add (append) that
integer to a list of integers. This is what I have thought of:

(defun add-int-to-list (intvar)
(setf *intlist* (append intvar *intlist*)))

But the interpereter doesn't like that: it says intvar not of type
list. So I tried to '(intvar) it and that just made a list of 'intvar.
eg: (intvar intvar intvar intvar...) I need help. please reply by
E-mail so that I get it quicker...

David Thornley

unread,
Jul 21, 1997, 3:00:00 AM7/21/97
to

In article <33D19CCA...@eng.utah.edu>,

An integer isn't a list, but an atom. Append is for lists, cons for
putting atoms onto lists.

(defun add-int-to-list (intvar)
(setf *int-list* (cons intvar *int-list*)))

David Thornley

Erik Naggum

unread,
Jul 21, 1997, 3:00:00 AM7/21/97
to

[comp.org.lisp-users removed from newsgroups]

* David Thornley -> Travish Robinson


| An integer isn't a list, but an atom. Append is for lists, cons for
| putting atoms onto lists.
|
| (defun add-int-to-list (intvar)
| (setf *int-list* (cons intvar *int-list*)))

is it useful to teach `setf', but not adjuncts like `push', `incf', etc?

also, is it useful to teach novices to define new functions with individual
side effects instead of idiomatic expressions for them?

I'm concerned because I see a lot of novice Lisp users here who ask the
oddest questions and are only given directions further along an obviously
harder trail than if they were asked to go back to camp and look more
closely at the map. I think this can contribute to needless frustration.

instead of writing a function to be called as (add-int-to-list 42), I think
a better way is to write (push 42 *int-list*) in place of the call, and
then ask the novice to macroexpand the form to see what it really does. I
don't know about others, but I found macroexpand to be a useful teacher,
especially when it came to not writing manually expanded macros when I
could use a supported macro or even think "there ought to be a macro way".

#\Erik"
--
there was some junk mail in my mailbox. somebody wanted to sell me some
useless gizmo, and kindly supplied an ASCII drawing of it. "actual size",
the caption read. I selected smaller and smaller fonts until it vanished.

Fred Haineux

unread,
Jul 21, 1997, 3:00:00 AM7/21/97
to

Travish Robinson <trob...@eng.utah.edu> wrote:
| (defun add-int-to-list (intvar)
| (setf *intlist* (append intvar *intlist*)))
|
| But the interpereter doesn't like that: it says intvar not of type
| list. So I tried to '(intvar) it and that just made a list of 'intvar.
| eg: (intvar intvar intvar intvar...) I need help. please reply by
| E-mail so that I get it quicker...

You need to crack open that Lisp reference book you have (and if you only
have Common Lisp the Language, get another, because although it's a fine
reference, there are much better ones for beginners!) and look up "cons"
and "append" and "list".

Especially, you WILL need to understand what a list "is" -- a set of
conses where the car of each is an element in the list, and the cdr of
each is a pointer to the next item in the list. You HAVE to know and
understand this COLD.

Obviously, whatever text you have has a chapter on this topic (but if you
don't have a text which covers this, you NEED ONE RIGHT NOW). Read it
again and again until you thoroughly understand this idea. It's the
cornerstone of many important computer science ideas, even if you intend
never to use Lisp again.

If your library has "The Little Lisper", go get that. It's a "drill" book
that teaches you what lists are. It's excellent, but once you've read it,
you won't need it, so borrow, not buy.

Holger Schauer

unread,
Jul 22, 1997, 3:00:00 AM7/22/97
to

>>"EN" == Erik Naggum schrieb am 21 Jul 97 17:27:10 GMT:

EN> also, is it useful to teach novices to define new functions with
EN> individual side effects instead of idiomatic expressions for
EN> them?

I don't think so.

EN> I'm concerned because I see a lot of novice Lisp users here who
EN> ask the oddest questions and are only given directions further
EN> along an obviously harder trail than if they were asked to go
EN> back to camp and look more closely at the map. I think this can
EN> contribute to needless frustration.

What you did not supply is a hint where a useful map can be found. I
think the FAQ might be a good starting point for searching for maps
but the best(*) map I have found so far is surely "ANSI Common Lisp"
by Paul Graham, Prentice Hall. With regard to your teacher
"macroexpand" I would like to add that the book also provides a good
introduction to macros.

Holger

(*) Best in the following sense: it is quite short, makes its (lots of
useful) points very clearly, is easy and with a lot of fun to
read. Let me add that this was the book I used last year when I
re-introduced myself to Lisp. Before I found it I took pretty deep
looks in at least five different books, neither of which was nearly as
useful as ACL (one of them was PAIP which is brilliant in its own
right. But ACL is better both as an introduction to non-AI-programmers
and as a quick reference).

0 new messages