0

Old C/C++ programmer, and I'm just starting to learn/play with scheme so my wording may not be correct but...

Let's say I do:

(define x 42)
(define y (quote x))

now using y how do I get to the 42?

I'm thinking of this as equivalent (in C terms) to:

int x=42;
int* y=&x;

Am I thinking about this wrong? If not what would be the scheme equivalent to *y?

  • I am still in the thrashing around stage of learning scheme. I do understand there is no inherent typing in scheme like there is in C, but since it is possible to assign the symbol to a variable there must be a way to get at the underlying value right? – epilitimus Oct 08 '20 at 20:13
  • This isn't really what symbols are for. Let's back up a step: *why* do you want to do this? Working on that actual problem will be more productive than trying to refine this unusual solution to it. – amalloy Oct 08 '20 at 21:26
  • As I said I am starting the process of learning scheme. When I read about symbols they struck me as analogous to pointers so I tried the above sequence of defines to test the theory, but I wasn't able to find any way to get at the underlying data. So if I understand your response symbols and pointers are **not** analogous? – epilitimus Oct 08 '20 at 21:39
  • There are no (explicit) pointers in Scheme, symbols are _not at all_ like pointers! Symbols are just immutable strings. – Óscar López Oct 08 '20 at 22:18
  • Okay. Back to head scratching then. Thankyou – epilitimus Oct 08 '20 at 23:18
  • In the posted code `y` is bound to the symbol `x`; when `y` is evaluated, it evaluates to the symbol `x`. If you pass `y` to a procedure, `y` is evaluated (because arguments to procedures are always evaluated) and its _value_ is passed to the procedure; that is, the _value_ `x` (which is a symbol) is passed to the procedure. You can call `eval` on `y`: `(eval y)` -> `42`. Here `y` evaluates to the symbol `x`, which is passed to `eval`, and `eval` evaluates the symbol `x`, which is bound to `42`. – ad absurdum Oct 09 '20 at 04:35
  • Does this answer your question? [What exactly is a symbol in lisp/scheme?](https://stackoverflow.com/questions/8846628/what-exactly-is-a-symbol-in-lisp-scheme) – Billy Brown Oct 09 '20 at 08:01

1 Answers1

0

Okay so to answer my question with the input from the above comments...

Symbols are not pointers, but can sort of be used like pointers.

The specific answer of how to get at the 42 is to use

(eval y)

or in guile (which is what I'm working with)

(eval y (interaction-environment))

see @ex nihilo's comment for why this works.

I found the guile version at How do I evaluate a symbol returned from a function in Scheme?

What symbols are is entries in the symbol table. A variable (itself an entry in the symbol table) which is bound to a symbol thus refers to another entry in the symbol table (analagous to pointers in this regard). However the referred to symbol need not exist at the time it is bound to the variable, but may be defined later because the linkage is not resolved until the symbol is evaluated. Thus

scheme@(guile-user)> (define z 'r)
scheme@(guile-user)> z
$1 = r
scheme@(guile-user)> (eval z (interaction-environment))
ERROR: In procedure memoize-variable-access!:
ERROR: Unbound variable: r

Entering a new prompt.  Type `,bt' for a backtrace or `,q' to continue.
scheme@(guile-user) [1]> ,q
scheme@(guile-user)> (define r #t)
scheme@(guile-user)> (eval z (interaction-environment))
$2 = #t

Not sure what I would ever use the later fact for but interesting none the less.

Thank you to all who helped me wrap my head around this one.