Simple functional programming in Max
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 (
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.
Have you seen the following thread ? http://www.cycling74.com/forums/topic.php?id=14524#post-77661 I guess you’ll find it interesting.
Don’t reinvent the wheel they say ;)
I actually remember trying those objects out ages ago, but I didn’t recall them today.
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).
----------begin_max5_patcher---------- 769.3ocyXtrbaCBEFds7SAUK6z3AP2yzMYY2zWfNY5fjvwzJAdLn1bYx6dEf UhchcrrQRtabBGgQ+7w+4.3ml44mKtmJ8AWC9Avy6oYddlP5.daZ64WStunh HMcyuPTWS4J+uXelhduxD+qWA9tPQuFbSUEPpHJ5hlJPIQQ.0MREHmBV0HWR KABNfn6Qwum2MJULNsPzvMCEdSvEBtRxdjpigvygaByapY7JpxHGzqAEMptn cckUZjlH+WWgR72ZX4jZyv5eyZFop6IqHphkL9c+bMsPYYBJMr8ECvo52eaq L8eBZiAtU+cdd1L8GewQ18s5UUT8Sj.0RJXAoPIzBCrngWnXs.K+APqnZVKY +gBJHUUxojcgmG6xR0zJFYPWDzIzwo+sUJuibJ.Cj667jtCa1PpGVQsSAeld 0B3mS324Ct88nAmcVnwZmvAV6DddTaC3vRlGqr4XmBbvmFbzjY+TI87LLnXa xlwpDY7MCMVtBfbmHnOxtrOfD5fKAkFLdtDc9Ca7yeNDWvPW.SXz3AlOeAbI HWJlDfPiGMDh45Mc.e5RTMAE6RMVnYSnHraESZpyoquf6yfvmECvglRo3PaB CL6BQg1ggU2Ta6ZxGhl1ucWWgmMsftPKD1ZYfiw9OWzLoXmvRLb.xjpoRI4N 563xXtq7dYQzYwh.ahTb7q0ZQoCqCQRq.vQzcXxZNjCIvApXOteP7Xj1nFhS obxaHicfFAQuTrcTphTSUKEkmVcDz.j2j3.ShSzelDtWjXdYlay9leOByvoi uKmjhl0EcRs63S.3KZtjJULNQe84s5Ddm9rjUVR4auWSMqbkn0OrQCGXQquR J5+NEg6ghRNMEEhzkACRs2y2VDX6Fwl6uX6k9+cbFjziYPvjxzvdnH8gXmPI 8lW2AbdYSpl5ijPSa1f9fhGUSS6Jm9Jw.zwV4ROMMgfQactkMWbd2V3XaPbj 4Bj5VCwL4XvMbZgaZe7fY6rBL5ZJquZBNsZZnMgNpof9HI3jKoisxE4fhZa7 7r+AT912vB -----------end_max5_patcher-----------
Here is an updated version in case anyone is interested.
Forums > MaxMSP