0

How can we get variable value with a string in scheme language as we can achieve this in Common Lisp:

> (defvar s 3) 
> S
> (symbol-value (intern "S"))
> 3

I am accessing a parameter of parent function from the closure.

EDIT: I have found this solution, but I can't use eval because it evaluates at top level. Searching for alternatives.

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

EDIT 2: I have found that Common lisp code also try to find symbol in global space. So this question is basically for both Lisps(Common Lisp, Scheme).

6
  • Are you performing string interpolation ? Commented Nov 29, 2017 at 12:22
  • 1
    You can't access a lexical variable in a lexical closure in Common Lisp via a string name. The DEFVAR example is about dynamic/special variables. Commented Nov 29, 2017 at 12:23
  • I realized that, so no solution? Commented Nov 29, 2017 at 12:25
  • 4
    Maybe it is an X-Y-Problem? meta.stackexchange.com/questions/66377/what-is-the-xy-problem You could have a certain problem, accessing a variable by name could be an approach - which then does not work. But there might be a solution for your original problem? Commented Nov 29, 2017 at 12:32
  • 1
    Common Lisp does not have a global space. The default package for interning symbols is usually user (often called cl-user). user uses the package lisp. It is possible to create packages that do not use lisp and creating a package that does not use lisp is a common error when learning about Common Lisp packages. Commented Nov 30, 2017 at 1:36

2 Answers 2

5

Don't do that!

Variables are for when you know the variable at compile time. In that case it is never a string. You can still reason about strings in compile time but your code also needs to have a relation with the name for it to be interesting. When you use eval or other forms that evaluate structure and compile/run data in runtime you are probably not doing it right (but not always. I've in my 20 year career used eval intentionally in production code twice)

If you want to store values you use a data structure. An assoc would mimic a dynamic environment. You can also use a hash with a level indicator if the size is harmless.

Sign up to request clarification or add additional context in comments.

Comments

1

You can't do what you want to do and in fact it is a confused thing to want to do.

Here's why what you are trying to do is confused. Consider how a Lisp system for which it was possible to do what you wanted would work. In particular consider something like this:

(define (foo a name)
  (let ([b 10])
    (display (get-value name))
    (* a b)))

Where get-value is meant to be how you get the binding of whatever something is.

So, if I call (foo 10 "b") it should print 10 and return 100.

But wait: b is a compile-time constant in this code. Any compiler worth its salt is going to immediately turn this into

(define (foo a name)
  (display (get-value name))
  (* a 10))

And now there is no binding of b.

So there are two options here: what you want to work works and it is impossible to ever write a reasonable compiler for Scheme, or what you want to work doesn't work, and it is.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.