I'm trying to make a patch which can simplify arithmetic expressions given as a single message. I want to, for example, send it the message "( 2 + 5 )" and get "7", or send it "foo ( 8 / 4 )" and get "foo 2". This much I have achieved, using [list-drip] and [route] to identify the data inside brackets, and [list-reduce] to perform the arithmetic. The difficulty I'm having is in getting it to work recursively, so that eg. "bar ( 5 - ( 3 - 2 ) )" gives "bar 1". Ideally, the abstraction would be able to process any number of bracket-levels, just as a scientific calculator does.
I know that programming recursively on PD is not for the faint hearted, and I feel pretty much out of my depth. If anyone can point me in the right direction, I will be most grateful. Alternatively, if it turns out to be too hard, I'll settle for what I've made already.
I'm attaching the patch, although I think it's pretty far removed from what the solution will look like. Note that I'm using Zexy and list-abs here.
If it were me I would patch a parser that did several passes iteratively instead of recursively:
depth expression, which are evaluated and substituted 3) Repeat 2 until the entire expression is reduced
Order of operations is a bit harder, but you could do that with multiple passes too.
On Mon, Nov 9, 2015 at 9:13 AM, Liam Goodacre liamg_uw@hotmail.com wrote:
I'm trying to make a patch which can simplify arithmetic expressions given as a single message. I want to, for example, send it the message "( 2 + 5 )" and get "7", or send it "foo ( 8 / 4 )" and get "foo 2". This much I have achieved, using [list-drip] and [route] to identify the data inside brackets, and [list-reduce] to perform the arithmetic. The difficulty I'm having is in getting it to work recursively, so that eg. "bar ( 5 - ( 3 - 2 ) )" gives "bar 1". Ideally, the abstraction would be able to process any number of bracket-levels, just as a scientific calculator does.
I know that programming recursively on PD is not for the faint hearted, and I feel pretty much out of my depth. If anyone can point me in the right direction, I will be most grateful. Alternatively, if it turns out to be too hard, I'll settle for what I've made already.
I'm attaching the patch, although I think it's pretty far removed from what the solution will look like. Note that I'm using Zexy and list-abs here.
Pd-list@lists.iem.at mailing list UNSUBSCRIBE and account-management -> http://lists.puredata.info/listinfo/pd-list