0
\$\begingroup\$

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.

Task

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.

Test Cases

  • Given 1 + 2 the output should be (+ 1 2)
  • Given 1 + 2 + 3 the output should be (+ 1 2 3)
  • Given 1 + (2 * 3) the output should be (+ 1 (* 2 3))
  • Given 2 * 3 + 4 the output should be (+ (* 2 3) 4)

This is , so shortest answer in bytes wins.

\$\endgroup\$
  • 3
    \$\begingroup\$ with input: 1 + 2 + 3, is outputting: (+ 1 (+ 2 3)) acceptable? \$\endgroup\$ – Felipe Nardi Batista Jul 25 '17 at 14:43
  • \$\begingroup\$ Translate to prefix notation might be more general, it's not Scheme-specific \$\endgroup\$ – Mr. Xcoder Jul 25 '17 at 14:46
  • \$\begingroup\$ @FelipeNardiBatista, no. Only if the original operation has parenthesis. \$\endgroup\$ – Kenny Barrera Jul 25 '17 at 14:46
  • 1
    \$\begingroup\$ @Mr.Xcoder i dont think that general prefix notation works with (+ 1 2 3) \$\endgroup\$ – Felipe Nardi Batista Jul 25 '17 at 14:47

Browse other questions tagged or ask your own question.