Skip to content

Instantly share code, notes, and snippets.

@dgageot

dgageot/RPN

Created Aug 4, 2010
Embed
What would you like to do?
foldingFunction=method(sum, x,
case(x,
"+", [sum pop! + sum pop!],
"*", [sum pop! * sum pop!],
"-", [sum pop! - sum pop!],
"/", [sum pop! / sum pop!],
[x toRational]
) + sum
)
"8 1 2 + 5 * +" split fold([], sum, x, foldingFunction(sum, x)) println
@dgageot

This comment has been minimized.

Copy link
Owner Author

@dgageot dgageot commented Aug 5, 2010

List foldingFunction = method(x,
append!(case(x,
or("+", "*", "-", "/"), Message fromText(x) appendArgument(pop!) sendTo(pop!),
x toRational
)
)
)
"8 1 2 + 5 * +" split prepend!([]) fold(foldingFunction) println

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment