Exponentiation order: Difference between revisions

m
→‎{{header|R}}: Improved syntax.
m (→‎{{header|R}}: Syntax highlighting.)
m (→‎{{header|R}}: Improved syntax.)
Line 776:
 
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 rsplus>inputs <- alist(5^3^2, (5^3)^2, 5^(3^2), 5**3**2, (5**3)**2, 5**(3**2))
invisible(sapply(inputs, function(x) cat(deparse(x), "returns: ", eval(x), "\n")))</lang>
 
Alternatively, we could print out a matrix or data frame:
<lang rsplus>print(matrix(sapply(inputs, eval), dimnames = list(inputs, "Outputs")))
print(data.frame(Inputs = sapply(inputs, deparse), Outputs = sapply(inputs, eval))))</lang>
{{out}}
<pre>> print(quote(5**3))
Line 802:
(5^3)^2 15625
5^(3^2) 1953125
> print(data.frame(Inputs = sapply(inputs, deparse), Outputs = sapply(inputs, eval)))
Inputs Outputs
1 5^3^2 1953125
331

edits