How to evaluate the character returned by a function in a schema?

I overestimate myself with the help of the Scheme, and I ran into a problem that probably reflects a fundamental misunderstanding on my part.

Let's say that I am doing the following in a schema (using Guile in this case, but this is the same in Chicken):

> (define x 5) > x 5 > (string->symbol "x") x > (+ 5 (string->symbol "x")) <unnamed port>:45:0: In procedure #<procedure 1b84960 at <current input>:45:0 ()>: <unnamed port>:45:0: In procedure +: Wrong type: x > (symbol? (string->symbol "x")) #t > (+ 5 x) ; here x is dereferenced to its value 5 10 > (+ 5 'x) ; here x is not dereferenced <unnamed port>:47:0: In procedure #<procedure 1c7ba60 at <current input>:47:0 ()>: <unnamed port>:47:0: In procedure +: Wrong type: x 

I understand that string->symbol returns the character x , which is effectively quoted. However, I cannot figure out how to use the character returned by string->symbol in any later context. How can I calculate this character?

To indicate why I want to do this, it is that I am writing a C program with a built-in Guile. I would like to be able to access the characters defined in Guile by name from C using, for example, scm_from_*_symbol or scm_string_to_symbol . The reasons why these functions do not work, as I thought, they will be related to my main question above. There may be a better way to do what I want to do with Guile, but that's another question. Now I am interested in a fundamental question.

+4
source share
3 answers

You should read the fly-evaluation chapter of the Guile documentation.

You want eval and probably interaction-environment

I recommend reading the famous SICP and Queinnec Lisp In small plays

+1
source

Characters are not special in this sense, that is, they are easier to evaluate than regular strings.

The character is very similar to a string, it just does not have quotes around it. Well, the fundamental difference is, of course, the absence of quotes, and the fact that the characters are interned. This means that the strings "x" and "x" are two different strings (although they are equal), while the characters 'x and 'x are actually the same object.

+3
source

What you want is to evaluate the symbol (and not "play" it). I think this is what you had in mind:

 (define x 5) (+ 5 (eval 'x (interaction-environment))) => 10 

See the documentation for more details.

+3
source

Source: https://habr.com/ru/post/1483938/


All Articles