The first programming language that I learned was Scheme. One of my first projects was to create an interpreter in Scheme that can take any basic arithmetic operation and translate it to Scheme prefix notation. Back then, I couldn´t sleep for some days.
The basic arithmetic operation in Scheme is like this:
(operator N1 N2 N3 N3 ... Nn)
Where operator is any of the basic operators (+ , - . * ,/), and N is any number or operation of the same form.
Given any basic arithmetic operation, translate it to Scheme prefix notation.
- Basic arithmetic operation means any operation that uses “+ , - . * , /” and “( )”
- The input is a string, and may or may not contain spaces between numbers and operators.
- The output should be in the form described before, and should give the correct result when operated in any online interpreter.
1 + 2the output should be
(+ 1 2)
1 + 2 + 3the output should be
(+ 1 2 3)
1 + (2 * 3)the output should be
(+ 1 (* 2 3))
2 * 3 + 4the output should be
(+ (* 2 3) 4)
This is code-golf, so shortest answer in bytes wins.