Simple functional programming in Max

Hans Höglund's icon

Hi everyone,

After working intensively on a bigger project, I hit the wall with a problem that required some simple recursive calls: unfortunately, that part had to be implemented in Max alone.

I came to muse on whether there were no easy way to enable functional style recursive calls in Max, and came up with three small abstractions that allows you to define and call a function (def, call and return). I've put them up with a sample factorial implementation.

There seem to be some problems with stack space (mostly in Overdrive mode), but they seem to work quite efficiently otherwise.

What do you think? Maybe this could be expanded into some set of functional utilities.

21.functional.zip
zip
jln's icon

Have you seen the following thread ? https://cycling74.com/forums/sharing-is-fun-oo-objects-for-max I guess you'll find it interesting.

Best,
jln

Hans Höglund's icon

Don't reinvent the wheel they say ;)

I actually remember trying those objects out ages ago, but I didn't recall them today.

Hans Höglund's icon

Actually, oo does not seem to support recusive calls, as seen in the patch below. Guess I have to stick to my objects for the moments (which use a simple zl stack to store return adresses and can call themselves as long as there is space on the stack).

Max Patch
Copy patch and select New From Clipboard in Max.

Hans Höglund's icon

Here is an updated version in case anyone is interested.

estevancarlos's icon

I'm interested in how this project went. Functional/reactive programming is gaining momentum with Javascript and I want to better understand how Max fits into that discussion of computer science concepts.