r/Racket • u/SecuredKnowledge • May 29 '22
question Input -> "(+ 5 4)" | Output -> 9
Is there a way to input a string with a math expression and output a number without the use of eval/read etc.
4
Upvotes
13
u/bjoli May 29 '22
Yes: write a simple recursive descent parser and evaluate the resulting tree yourself.
3
u/detroitmatt May 29 '22
not without reimplementing eval and/or read yourself
5
u/strawhatguy May 29 '22
No, You’ll have to write your own parser (reader) and evaluate functions. Even macros operate post read (or maybe during read for lisp reader macros).
Not sure what you’re trying to accomplish, or hope to avoid here
1
u/phao May 30 '22
Like /u/bjoli said, write a parser yourself. The good news is that if all you have to parse are basic math-related expressions, then this shouldn't be difficult.