1

I have encountered a problem where I would want to evaluate a math equation, let's say a list of string ' (1 + 2) and convert it into (+ 1 2) so that racket can solve it. Apparently, you can not simply do ('+ 1 2) as '+is not a procedure. What are some ways to do it?

Gerry G
  • 48
  • 5

1 Answers1

2

After transforming the expression from infix to prefix notation, just use eval:

(define ns (make-base-namespace))
(eval (list '+ 1 2) ns)
=> 3

Of course the usual warning applies, eval is evil, etc. But it's ok for learning purposes, as long as you're aware that most of the time you should not use it in real-life programs.

Óscar López
  • 232,561
  • 37
  • 312
  • 386
  • @GerryG did this post answer your question? if so, please don't forget to [accept](https://meta.stackexchange.com/questions/5234/how-does-accepting-an-answer-work) it, simply click on the check mark to the left ;) – Óscar López Jan 28 '19 at 18:20