Re: (dsssl) access variable value with a string

Subject: Re: (dsssl) access variable value with a string
From: Brandon Ibach <bibach@xxxxxxxxxxxxxx>
Date: Fri, 16 Mar 2001 02:17:21 -0600
Quoting hoenicka_markus <hoenicka_markus@xxxxxxxxxxxxxx>:
> (define styleparams
>   (list
>     (cons 'foo foo)
>     (cons 'bar bar)))
> 
> (define (symbol-value varname)
>   (let ((a (assoc (string->symbol varname) styleparams)))
>     (if a (cadr a) #f)))
> 
> Then I try to get the value of say foo with:
>  (symbol-value var)
> where var is the string "foo".
> 
> Jade says:
> openjade:E: 1st argument for primitive "list-ref" of wrong type: "(foo .
> "none")" not a list
> 
> Could anyone enlighten me how I get this straight?
> 
   Actually, I think this is just a simple matter of confusion on the
nature of lists.  A list is a special type of pair.  A pair is a
structure with two slots known as "car" and "cdr", the contents of
which can be accessed using the functions with those names.  A pair is
generally represented as:  (car . cdr)
   A list is a pair in which the cdr of the pair is another list.  To
avoid a list recursing forever, the special "empty list" is provided
to terminate it.  So, a list such as (a b c d) would actually be
represented in pair notation as:  (a . (b . (c . (d . ()))))
   So, the pair you have:  (foo . "none")  is not really a list,
because you have a string ("none") in the cdr slot.  I'm not positive,
but I'm guessing that the error message is originating from within the
(cadr a) call.  The (cadr) function is probably implemented as:
     (define (cadr l) (list-ref l 1))
   Keep in mind that (cons) creates a pair, not a list (unless the
second argument to it happens to be a list).  If you change the (cadr)
to just (cdr), I think it'll work. :)

-Brandon :)

 DSSSList info and archive:  http://www.mulberrytech.com/dsssl/dssslist

Current Thread