Forums > MaxMSP

# My first patch, and a question

February 3, 2009 | 2:23 pm

When I learn a new programming language, I like to start by writing a program to generate Fibonacci numbers. So I’m working my way through the tutorials and think I have learned enough to solve this simple problem.

Interestingly, it took me a surprisingly long time to figure out how to do this — I kept getting confused about how to order the various operations. What I finally realized is that a Number object really corresponds to the right-hand side of an assignment, because when it gets a bang, it sends its value to a destination. Which means that I have to arrange for the various operations to be executed in an order that depends on the right-hand sides of the respective assignemnts, rather than the left-hand sides.

The attached patch almost solves the problem, but to start it off, you have to manually set the topmost Number object to 1. After that, each click of the button generates another Fibonacci number and prints it.

So my questions are:

1) Can this patch be simplified?
2) How do I get the Number object to start out equal to 1 when the patch is loaded?

February 3, 2009 | 2:45 pm

You could use [loadmess 1] connected to your [number] object. It is important to remember the order that max sends its messages, for example try moving the boxes connected to your [button] around and see if the patch still works. If you want to specify the order of operation regardless of object position you can use [trigger].

lh

February 3, 2009 | 3:26 pm

To be honest, i think your solution is quite smart. But the amount of connections and order-dependancy makes it hard to read. As ‘hope’ mentioned, do use the [t] to make operations happen in a certain order. Even if the next objects are beside each other and not above/below, it helps you read the patch later on.

I remade your patch here. The two integer objects store the data (the right one being the lowest number). Upon bang, values are added and two newest numbers are stored again in the integer objects. The result is then printed. You can remove all the trigger objects here; none are necessary. I think both versions are easier to read than your version though^^.

I’d advise against using [number] to store integers, use [i] instead. It can be set with the right inlet, initialised, and it doesn’t repaint itself (which slows your patch down when you use 100s of them). I use [number] for debugging and interfaces only.

Good luck maxing!

– Pasted Max Patch, click to expand. –

February 3, 2009 | 3:29 pm

February 3, 2009 | 3:54 pm

Ah, I don’t think I’ve reached the tutorial that talks about [int] objects yet, which would explain why I didn’t use them :-)

The tutorial took great pains to explain how operations are ordered if you don’t specify an ordering, so I assumed that it was considered normal to take advantage of those ordering properties. I guess that’s not quite the case?

February 3, 2009 | 4:48 pm

"The tutorial took great pains to explain how operations are ordered if you don’t specify an ordering, so I assumed that it was considered normal to take advantage of those ordering properties. I guess that’s not quite the case?"

In my (very limited) experience, once you’ve had enough hangups and glitches due to ordering, it becomes second nature and you will start considering it when you lay out patches, but it shouldn’t be something that you stress over. I only specify an order of execution when its necessary.

February 3, 2009 | 4:55 pm

OK, here’s my second try:

– Pasted Max Patch, click to expand. –

My goal here is to learn how to speak Max without an accent :-)

February 3, 2009 | 5:20 pm

This is a scenario you want to avoid:
-you don’t use a [t b b] and rely on a left-right order.
-three months later, you open the patch, update it to add some functionality
or correct a bug, and while doing that move around objects to make the patch
prettier. But then, your graphically-based sequence may not work anymore,
and the bug may be hard to trace.

February 3, 2009 | 5:28 pm

Uh, sure you copied your new version? It looks the same as the patch i posted earlier.

And i agree with jeanfrancois, these things may look trivial now, but readability of patches is very important in bigger projects…i try to always use triggers and never leave things up to the layout.

February 3, 2009 | 5:30 pm

Quote: Bas van der Graaff wrote on Tue, 03 February 2009 10:28
—————————————————-
> Uh, sure you copied your new version? It looks the same as the patch i posted earlier.

I’ll check.

—————————————————-
> And i agree with jeanfrancois, these things may look trivial now, but readability of patches is very important in bigger projects…i try to always use triggers and never leave things up to the layout.

That is what I would have thought, too; which is why I was surprised that the tutorial emphasized ordering so much.

I’m going to try again and repost shortly.
—————————————————-

February 3, 2009 | 5:46 pm

OK, here’s my next try…

– Pasted Max Patch, click to expand. –

I think I actually posted the right patch this time :-)

February 3, 2009 | 6:18 pm

and to avoid the double 1 at the beginning…

p

– Pasted Max Patch, click to expand. –

http://www.trblnc.org

February 3, 2009 | 6:21 pm

Quote: pure wrote on Tue, 03 February 2009 11:18
—————————————————-
> and to avoid the double 1 at the beginning…

But the double 1 at the beginning is correct — the Fibonacci sequence is 1, 1, 2, 3, 5, etc.

February 3, 2009 | 6:40 pm

Quote: ark wrote on Tue, 03 February 2009 08:23
—————————————————-
> When I learn a new programming language, I like to start by writing a program to generate Fibonacci numbers. So I’m working my way through the tutorials and think I have learned enough to solve this simple problem.
>

———————————————-

just as another method, using a closed formula instead of the recurring sequence. This generates the ratio needed to find the next term, plus gives an example of [expr] being handy in some cases to combine multiple operations into one statement. in [expr], as in other languages, the order is based upon traditional ordering (parentheses, powers, mult/div, add/subtract etc.), whereas using single objects the order is determined by the patchcord flow. Interesting to think about it in multiple ways, as sometimes one way is easier or more clear than the other.

along the original lines, as stated here, trigger will definitely be your friend in the future.

– Pasted Max Patch, click to expand. –

February 3, 2009 | 9:25 pm

Here’s my version.

lh

– Pasted Max Patch, click to expand. –

February 4, 2009 | 4:50 pm

That’s cute! And I have no trouble understanding how it works, once I’ve right-clicked on the objects I don’t understand.

But I’ve only worked through a few of the tutorials yet, and hadn’t come to zl or unpack, which explains why I did it more straightforwardly :-)

Viewing 16 posts - 1 through 16 (of 16 total)