I think that the main thing that's confusing you is that you are using an
association list incorrectly. First, it is a list of dotted lists, so:
(defconstant *month*
'((jan . 31)
(feb . 29)
(mar . 31)
(apr . 30)
(may . 31)
(jun . 30)
(jul . 31)
(aug . 31)
(sep . 30)
(oct . 31)
(nov . 30)
(dec . 31)))
And of course it isn't a function, it's a constant- so use defconstant,
not defun!
Now to get a sublist out use the assoc (assoc item a-list) function which
returns the first dotted sublist that it hits in which the first element
is eql to the item. So given this,
> (assoc *month* 'feb)
gives
(feb . 29)
So to get the number of days in February, you want
> (cdr (assoc *month* 'feb))
and you get
29
Then of course you need to fix the program so that it counts correctly
(but I expect you knew that).
General hints:
Make sure each function is doing what you want it to! This includes the
ones you build *and* the ones you use. I personally have a bizarre
inability to remember the order of arguments to #'>, and so I can't tell
you how many times I have had to type (> 5 3) into the Listener just to
see if it is T or NIL. One of the beauties of an interpreted language is
that this is so incredibly easy to do. So, especially the first time you
use a function, just *try* it.
Get a copy of Steele (it's online FREE in html nowadays- I don't know
where offhand but I bet there's a pointer on www.digitool.com). The full
title is Common Lisp the Language, 2nd edition, by Guy L. Steele;
published by Digital Press. It is commonly referred to as Steele or CLtL2.
It is an indespensible
tool for the Lisp programmer, and also a nice source of amusements during
those long nights...
Good luck!
Ethan
In article <··········@news1.usa.pipeline.com>, ····@usa.pipeline.com(Boaz
Chow) wrote:
> would you tell me what is wrong with this program please?
> If I type :
>
> (days 1 jan 2 jan)
>
> I will get 2
>
> but if I type :
>
> (days 1 jan 2 feb)
>
> I will get NIL or NULL
>
> (defun *month* '( ( jan 31)
> (feb 29)
> (mar 31)
> (apr 30)
> (may 31)
> (jun 30)
> (jul 31)
> (aug 31)
> (sep 30)
> (oct 31)
> (nov 30)
> (dec 31) ))
>
> (defun days (d1 m1 d2 m2)
> (if (eql m1 m2) (- d2 d1) (assoc m1 *month*)
> )
> )
Ethan
_____________
Ethan Benatan
······@pitt.edu http://www.pitt.edu/~ethan