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.


Han-Wen Nienhuys <hanwen@cs.uu.nl> writes:

 > I have a function get_property () that basically boils down to
 > 
 >       SCM p = scm_assq (gh_symbol2scm (keyname), alist)
 >       return p == SCM_BOOL_F ? SCM_UNDEFINED : gh_cdr (p)
 >
 > Removing a property amounts to
 > 
 > 	 alist = gh_acons (key_symbol, SCM_UNDEFINED, alist);
 >
 > 
 > If I were to use assq directly, I would have to do
 > 
 >       SCM s = assq  ( [stuff] );
 >       if (s != SCM_BOOL_F &&  gh_number_p (gh_cdr (s)))
 > 	 {
 > 		s = gh_cdr (s);
 > 		[do special stuff]
 > 	 }
 > 
 > which is longer, more clumsy and error-prone.  More importantly, I
 > don't have a means to erase a value from an alist, because my alists
 > are read-only. 

Why are you talking about using assq directly given that you have a
get_property function wrapping it?  What's the point of that?

In any case, you're assuming that #f is never the value of a property,
correct?  Otherwise you'll treat the property as unset.  Why not then
use #f as the value that denotes unset instead of SCM_UNDEFINED?  Then
everything goes through cleanly with assq_ref.

If, on the other hand, the inability to use #f as a value is a
shortcoming you want to overcome, you can make up a special value
that can be in the list to denote undefined:

(define *my-undefined* (cons (gensym) (gensym)))

The cons creates a new cons cell.  That it contains two (gensym)s
means that it can never be eq? to something a value that anyone's
using unless you give it to them.

Then you can define get_property, set_property & delete_property as
follows:

(define (get_property key alist)
   (let ((p (assq key alist)))
      (if p
          (cdr p)
          *my-undefined*)))

(define (set_property key value alist)
   (cons (cons key value) alist))

(define (delete_property key alist)
   (set_property key *my-undefined* alist))

(define (defined_property? key alist)
   (eq? *my-undefined* (get_property key alist)))

-- 
Harvey Stein
Bloomberg LP
hjstein@bfr.co.il

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