treating variable names as strings (NEWBIE) 
Author Message
 treating variable names as strings (NEWBIE)

I confess that I'm a complete newbie to Scheme. I'm trying to write a simple
address book program, and I was planning on having the address book as a list
of variable names which can be evaluated to the variables themselves (a list
of things like names, telepohne numbers etc.). I was planning to store them in
alphabetical order (seemed like a good compromise between efficiency and
programming difficulty) and use procedures like string>? to compare them.
Unfortunately, I can't work out how to treat the variables as strings for this
purpose. I can't find anything in my scheme book, and I had a look in r5rs but
couldn't find anything there either.

Could anyone fill me in? Or suggest something different?

Of course, I could always include a string in the entry lists that happens to
be the same as the name of the list, but that seems like a crude work around.

--

Andrew Tarr
"To Absent friends, lost loves, old gods, and the season of mists, and may
each and every one of us always give the Devil his due"
-- Neil Gaiman, _Sandman_, *The Seasons of Mists*



Sat, 20 Apr 2002 03:00:00 GMT  
 treating variable names as strings (NEWBIE)

Quote:
>I confess that I'm a complete newbie to Scheme. I'm trying to write a simple
>address book program, and I was planning on having the address book as a list
>of variable names which can be evaluated to the variables themselves (a list
>of things like names, telepohne numbers etc.). I was planning to store them in
>alphabetical order (seemed like a good compromise between efficiency and
>programming difficulty) and use procedures like string>? to compare them.
>Unfortunately, I can't work out how to treat the variables as strings for this
>purpose. I can't find anything in my scheme book, and I had a look in r5rs but
>couldn't find anything there either.

Take a look at

 symbol->string
 string->symbol

-Steve



Sat, 20 Apr 2002 03:00:00 GMT  
 treating variable names as strings (NEWBIE)

Quote:


> >I was planning on having the address book as a list
> >of variable names which can be evaluated to the variables themselves (a list
> >of things like names, telepohne numbers etc.).

Do you want variables?  Or symbols?  Why not just use strings to begin with?

Quote:
> > I was planning to store them in
> >alphabetical order (seemed like a good compromise between efficiency and
> >programming difficulty) and use procedures like string>? to compare them.

The Scheme compiler decides the order in which variables are stored.
If you make your own table, you don't put in it variables, just symbols or
strings or numbers...

Quote:
> >Unfortunately, I can't work out how to treat the variables as strings for this
> >purpose.

Variables are just arbitrary names used in a program.  You are trying
to build a data structure.

Quote:
> > I can't find anything in my scheme book, and I had a look in r5rs but
> >couldn't find anything there either.

> Take a look at

>  symbol->string
>  string->symbol

Doesn't get you from variables to strings, but it may be what
ey wants.

--

Programmer in Chief, Free Computer Shop <http://www.free-comp-shop.com>
         ---  Food, Shelter, Source code.  ---



Sat, 20 Apr 2002 03:00:00 GMT  
 treating variable names as strings (NEWBIE)

Quote:

> I confess that I'm a complete newbie to Scheme. I'm trying to write a simple
> address book program, and I was planning on having the address book as a list
> of variable names which can be evaluated to the variables themselves (a list
> of things like names, telepohne numbers etc.). I was planning to store them in
> alphabetical order (seemed like a good compromise between efficiency and
> programming difficulty) and use procedures like string>? to compare them.
> Unfortunately, I can't work out how to treat the variables as strings for this
> purpose. I can't find anything in my scheme book, and I had a look in r5rs but
> couldn't find anything there either.

I think eval might do what you want; I think it's nonstandard, but fairly
widespread.  As the name suggests, it evaluates its argument, so:

(define aa 42)
(eval 'aa)

-or-

(define aa 42)
(eval (string->symbol "aa"))

would return 42.  I hope this helps.

--Jed

"But life wasn't yes-no, on-off.  Life was shades of gray, and rainbows not
in the order of the spectrum."   -- L. E. Modesitt, Jr., _Adiamante_

to resolve e-mail, add .oberlin.edu



Tue, 23 Apr 2002 03:00:00 GMT  
 treating variable names as strings (NEWBIE)


Quote:
>(define aa 42)
>(eval (string->symbol "aa"))

>would return 42.  I hope this helps.

This example assumes that Scheme uses lowercase letters to represent
symbols.  The Report requires that a single case be used internally,
but doesn't specify which must be used.  A conformant implementation
could give an error for this example, but work on

(eval (string->symbol "AA"))



Sat, 04 May 2002 03:00:00 GMT  
 treating variable names as strings (NEWBIE)
Quote:



>>(define aa 42)
>>(eval (string->symbol "aa"))

>>would return 42.  I hope this helps.

> This example assumes that Scheme uses lowercase letters to represent
> symbols.  The Report requires that a single case be used internally,
> but doesn't specify which must be used.  A conformant implementation
> could give an error for this example, but work on

> (eval (string->symbol "AA"))

Oops.  I forgot about that.
So then:

(eval `(define ,(string->symbol "aa") 42))
(eval (string->symbol "aa"))

should work.

This could also be done with macros:

(define-macro
  (sv-define name val)
    `(eval (list 'define (string->symbol ,name) ,val)))

(define-macro
  (sv-set! name val)
    `(eval (list 'set! (string->symbol ,name) ,val)))

(define-macro
  (sv-ref name)
    `(eval (string->symbol ,name)))

...but this is starting to get a bit ugly.

--Jed

to resolve e-mail add .oberlin.edu



Mon, 06 May 2002 03:00:00 GMT  
 
 [ 6 post ] 

 Relevant Pages 

1. treat value in a variable as a variable

2. Using a variable name for a variable name??

3. Procedure name in a string variable

4. Using variable names as strings

5. String to Variable Name Mapping

6. Variable names in text strings

7. Variable name -> string

8. creating variable names by adding 2 strings?

9. how to convert variable name to a string?

10. string to variable name

11. More About Array Names In String Variables

12. passing args to proc is being treated as an entire string

 

 
Powered by phpBB® Forum Software