Skip to main content
added 2 characters in body
Source Link
rturnbull
  • 3.9k
  • 16
  • 27

R, 140142 bytes

R is able to parse itself, so rather than re-invent the wheel, we just put the parser to work, which outputs prefix notation, and use a recursive function to switch it to postfix notation.

functionf=function(x,p=1){
if(p)x=match.call()[[2]]
if((l=length(x))>1){
f(x[[2]],0)
if(l>2)f(x[[3]],0)
if((z=x[[1]])!="(")cat(z,"")
}else cat(x,"")
}

The p argument is to control the use of non-standard evaluation (the bane of R programmers everywhere), and there are a few extra ifs in there to control the outputting of brackets (which we want to avoid).

Input: (0-1+(2-3)*4-5*(6-(7+8)/9+10))

Output: 0 1 - 2 3 - 4 * + 5 6 7 8 + 9 / - 10 + * -

Input: (((((1))) + (2)))

Output: 1 2 +

As a bonus, it works with arbitrary symbols, and any pre-defined functions with up to two arguments:

Euler's identity

Input: e^(i*pi)-1

Output: e i pi * ^ 1 -

Dividends of 13 between 1 and 100

Input: which(1:100 %% 13 == 0)

Output: 1 100 : 13 %% 0 == which

Linear regression of baby chicken weight as a function of time

Input: summary(lm(weight~Time, data=ChickWeight))

Output: weight Time ~ ChickWeight lm summary

The last example is perhaps a little outside the scope of the OP, but it does use postfix notation, so...

R, 140 bytes

R is able to parse itself, so rather than re-invent the wheel, we just put the parser to work, which outputs prefix notation, and use a recursive function to switch it to postfix notation.

function(x,p=1){
if(p)x=match.call()[[2]]
if((l=length(x))>1){
f(x[[2]],0)
if(l>2)f(x[[3]],0)
if((z=x[[1]])!="(")cat(z,"")
}else cat(x,"")
}

The p argument is to control the use of non-standard evaluation (the bane of R programmers everywhere), and there are a few extra ifs in there to control the outputting of brackets (which we want to avoid).

Input: (0-1+(2-3)*4-5*(6-(7+8)/9+10))

Output: 0 1 - 2 3 - 4 * + 5 6 7 8 + 9 / - 10 + * -

Input: (((((1))) + (2)))

Output: 1 2 +

As a bonus, it works with arbitrary symbols, and any pre-defined functions with up to two arguments:

Euler's identity

Input: e^(i*pi)-1

Output: e i pi * ^ 1 -

Dividends of 13 between 1 and 100

Input: which(1:100 %% 13 == 0)

Output: 1 100 : 13 %% 0 == which

Linear regression of baby chicken weight as a function of time

Input: summary(lm(weight~Time, data=ChickWeight))

Output: weight Time ~ ChickWeight lm summary

The last example is perhaps a little outside the scope of the OP, but it does use postfix notation, so...

R, 142 bytes

R is able to parse itself, so rather than re-invent the wheel, we just put the parser to work, which outputs prefix notation, and use a recursive function to switch it to postfix notation.

f=function(x,p=1){
if(p)x=match.call()[[2]]
if((l=length(x))>1){
f(x[[2]],0)
if(l>2)f(x[[3]],0)
if((z=x[[1]])!="(")cat(z,"")
}else cat(x,"")
}

The p argument is to control the use of non-standard evaluation (the bane of R programmers everywhere), and there are a few extra ifs in there to control the outputting of brackets (which we want to avoid).

Input: (0-1+(2-3)*4-5*(6-(7+8)/9+10))

Output: 0 1 - 2 3 - 4 * + 5 6 7 8 + 9 / - 10 + * -

Input: (((((1))) + (2)))

Output: 1 2 +

As a bonus, it works with arbitrary symbols, and any pre-defined functions with up to two arguments:

Euler's identity

Input: e^(i*pi)-1

Output: e i pi * ^ 1 -

Dividends of 13 between 1 and 100

Input: which(1:100 %% 13 == 0)

Output: 1 100 : 13 %% 0 == which

Linear regression of baby chicken weight as a function of time

Input: summary(lm(weight~Time, data=ChickWeight))

Output: weight Time ~ ChickWeight lm summary

The last example is perhaps a little outside the scope of the OP, but it does use postfix notation, so...

Source Link
rturnbull
  • 3.9k
  • 16
  • 27

R, 140 bytes

R is able to parse itself, so rather than re-invent the wheel, we just put the parser to work, which outputs prefix notation, and use a recursive function to switch it to postfix notation.

function(x,p=1){
if(p)x=match.call()[[2]]
if((l=length(x))>1){
f(x[[2]],0)
if(l>2)f(x[[3]],0)
if((z=x[[1]])!="(")cat(z,"")
}else cat(x,"")
}

The p argument is to control the use of non-standard evaluation (the bane of R programmers everywhere), and there are a few extra ifs in there to control the outputting of brackets (which we want to avoid).

Input: (0-1+(2-3)*4-5*(6-(7+8)/9+10))

Output: 0 1 - 2 3 - 4 * + 5 6 7 8 + 9 / - 10 + * -

Input: (((((1))) + (2)))

Output: 1 2 +

As a bonus, it works with arbitrary symbols, and any pre-defined functions with up to two arguments:

Euler's identity

Input: e^(i*pi)-1

Output: e i pi * ^ 1 -

Dividends of 13 between 1 and 100

Input: which(1:100 %% 13 == 0)

Output: 1 100 : 13 %% 0 == which

Linear regression of baby chicken weight as a function of time

Input: summary(lm(weight~Time, data=ChickWeight))

Output: weight Time ~ ChickWeight lm summary

The last example is perhaps a little outside the scope of the OP, but it does use postfix notation, so...