Frank Lay | 18 Dec 2011 00:52
Picon

Reading primitive procedures

Hello,

I don't understand the following Scheme behaviour:

> (read)
+
'+
> (read)
1 +
1
#{Procedure 93 +}

Why in the second interaction is the input '+' read as a procedure, but in the first as a symbol?

I am very new to Scheme, so my apologies if this is a silly question. I'm trying to write an RPN interpreter in Scheme, and I thought I'd apply the procedure read from the input to a stack of operands, however, because of the first case, this method isn't robust. I don't understand how to 'unsymbolise' a symbol into its corresponding procedure. Maybe there is a better way to do this?

Thanks for reading.

RT Happe | 18 Dec 2011 02:54
Picon

Re: Reading primitive procedures

Frank Lay asked:

> > (read)
> 1 +
> 1
> #{Procedure 93 +}
> 
> Why [...] is the input '+' read as a procedure

When you hit enter after the +  the READ call eats the first datum
and returns the corresponding value, the number 1.
Then the REPL reads and evaluates the second datum, +.
I.e. you did basically the same as

  > (read)
  1
  > +
  #{Procedure 93 +}

In the words of the R5RS,

  READ returns the next object parsable from the given input port,
  updating port to point to the first character past the end
  of the external representation of the object.

> I don't understand how to 'unsymbolise' a symbol into its corresponding procedure.

Have a look at the EVAL procedure, the E in REPL, as in

  (eval '+ (scheme-report-environment 5))

Alternatively & depending on your plans, you could evaluate function symbols yourself,
e.g. map yourself the symbol '+  to the procedure +.

Disclaimer: I am not up to date as to the conformance of s48 with R*RS.

rt


Gmane