Exponentiation order: Difference between revisions

→‎{{header|R}}: Added what I believe to be the final possible method.
(→‎{{header|R}}: Significantly simplified metaprogramming and provided two different output methods.)
(→‎{{header|R}}: Added what I believe to be the final possible method.)
Line 731:
<lang r>print(quote(5**3))
print(quote(5^3))</lang>
 
Another method is to use "^" as if it is an ordinary function of two arguments. It appears that "**" does not support this. As there is no potential for ambiguity in the operator precedence, we will not print this result below. For example:
<lang r>'^'('^'(5, 3), 2)</lang>
is clearly (5^3)^2 i.e. 15625, whereas
<lang r>''^'(5, '^'(3, 2))</lang>
is clearly 5^(3^2) i.e. 1953125.
 
As for actually solving the task, the requirement that each output be on a new line causes us a surprising amount of difficulty. To avoid repeating ourselves, we must almost resort to metaprogramming:
<lang r>inputs<-alist(5^3^2, (5^3)^2, 5^(3^2), 5**3**2, (5**3)**2, 5**(3**2))
331

edits