This is the mail archive of the guile@sourceware.cygnus.com mailing list for the Guile project.


Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]

Re: another nit.


Dirk Herrmann <dirk@ida.ing.tu-bs.de> writes:

> On Thu, 6 Jul 2000, Han-Wen Nienhuys wrote:
> 
> > why do assq-ref and friends return #f when the key is not found, in
> > stead of #<unspecified>? This makes it kind of hard to distinguish
> > between an alist not containing KEY and  (KEY . #f)

This entire thread is based on a misunderstanding by Han-Wen.  assq
does NOT return just the cdr of the pair whose car is KEY, but the
whole pair, and therefor it's quite easy to see if the alist did NOT
contain the key at all, or if it's value is #f.

So, if the value of a key is #f, you would get back (KEY . #f), but if
the key was not found at all you would get back #f.  If the value was
something else, like 3, you would get (KEY . 3).

An excerpt from R5RS

[[library procedure]] (assq obj alist)
[[library procedure]] (assv obj alist)
[[library procedure]] (assoc obj alist)

Alist (for "association list") must be a list of pairs. These
procedures find the first pair in alist whose car field is obj, and
returns that pair. If no pair in alist has obj as its car, then #f
(not the empty list) is returned. Assq uses eq? to compare obj with
the car fields of the pairs in alist, while assv uses eqv? and assoc
uses equal?.

Code for determine if the list is empty:

(define my-assoc (key alist)
  (let ((val (assoc key alist)))
     (if (pair? val) val (error "No friggin value for that key"))))


Please note I ain't written scheme for awhile so be easy on me.

-- 
Craig Brozefsky               <craig@red-bean.com>
Lisp Web Dev List  http://www.red-bean.com/lispweb
---  The only good lisper is a coding lisper.  ---

Index Nav: [Date Index] [Subject Index] [Author Index] [Thread Index]
Message Nav: [Date Prev] [Date Next] [Thread Prev] [Thread Next]