Writing a class block with calling a method of itself in Snap! 12.0.6

Chapter 1. About the Y-Combinator

The Y-Combinator is for recursive functions when the function cannot be named.

The mathematical definition for the Y-combinator is:

Y=\lambda f.(\lambda x.x\ x)\ (\lambda x.f(\lambda y. x\ x\ y))

Where (\lambda x.x\ x) is the U-Combinator (Function self-caller), and (\lambda x.f(\lambda y.x\ x\ y)) is the combinator function. The \lambda y. x\ x\ y wrap is the “Evaluation delay”.

The argument for a Y-Combinator looks similar to this:

Y\ (\lambda f.\lambda x.\text{function contents using}\ x\ \text{and}\ f)

Where f is the recursive function, and x is the argument.

And, the Snap! 12.0.6 definition for the Y-Combinator block looks like the photo in Figure 1-1:

Make sure you build this for the next chapter for writing a class block.

The example is included in our other project “Y-Combinator Revised”, but for this topic, we will borrow the script from that project as an example.

Figure 1-2 is an alternative ring block for calculating factorials using a Y-Combinator.

This block can be evaluated by using the “call” block once again.
If you drag the “call” block into the first input slot, right-click the block, click “unringify,” and then expand the parameter slot, this block becomes able to evaluate.

Additionally, using the list function, you can define custom blocks with more than one parameter without a name, even if there is only one parameter, as if there were more than one.

In Figure 1-3, if you try the same method as above, that block becomes able to evaluate again. If you put a list block into the first parameter input slot, that block works.

Chapter 2. Writing a class block using the Y-Combinator in Snap! 12.0.6

To write a class block with calling a method of itself inside a method, without any this script block, you must need a Y-Combinator block.

First, create a new block. You can name the block to anything you want. In this topic, we will create the new counter block. Its definition looks like the photo in Figure 2-1 if you read the manual and try to code:

There are missing input slots labeled ???. So this is the reason why you should use a Y-Combinator to write a class block with calling a method of itself inside a method.

We can use the Y-Combinator to write a class block with the function that we mentioned. For many arguments in the ring, we can use the first-class lists. Now we can code using the similar thing for a Y-combinator to create it. The full definition now looks like the photo in Figure 2-2:

Press OK, and run the following code, and check the output in Figure 2-3:

If it works properly, You have mastered the Snap! OOP with Y-Combinators.

Chapter 3. Off-topics

Chapter 3-1. Off-topic 1

You can get the name of the language translator by building like the photo shown in Figure 3-1-1:

Then, right-click the block in the second block and select the “rename…” option from the menu. When you open the variable name menu, “language_translator” appears as the person who translated the project editor content, as shown in Figure 3-1-2.

Note that the variable name shown in the photo below, with its translated content, is not my name.

This is a good start, but I think it needs work.

when the function cannot be named

“Cannot” confuses me, because it sounds as if the point is to get around a deficiency in your programming language. I prefer to think “isn’t it cool that we can create a recursive procedure without naming it?” But I’m not saying you have to use my preferred text; better than either yours or mine would be to start with a specific example, first showing a recursive function definition with a name, then showing that it’s possible also to define the same recursive function without naming it. You might also make it explicit that the application you have in mind is recursive methods of an OOP class. (But I would show a first example not in an OOP context, just doing a Y-based factorial or something.)

Y=λf.(λx.x\ x) (λx.f(λy.x\ x\ y))

Your reader doesn’t know what a U-combinator is, namely, a way to call a function with itself as its input. I would also say “f is the input to the Y combinator, and is a function written to take itself as input along with whatever other inputs it needs. x is what that function is named inside the Y combinator. y is the ordinary data input to the function you want.”

Actually at this point I want to back up and ask what you assume your reader does and doesn’t know. Do they know that λ is the same as a gray ring? Do they know that in lambda calculus all functions have exactly one input, and you give more than one input by currying? Since Snap! functions have multiple inputs, that’s not an obvious convention.

Ordinarily I’m in favor of self-documenting variable names, but this picture is harder to parse than necessary because its variables don’t match the variables in the Y combinator formula. Maybe you could have your cake and eat it too by including the formula variable in the name of the script variable: x (acc), f (self), and so on. (It occurs to me as I say this that your formula has two different variables named x.)

The argument for a Y-Combinator looks silimar to this:

“similar” misspelled.

Where f is the recursive input, and x is the argument.

I’d say f is the recursive function. Also, don’t capitalize the W.

For many arguments in the ring, we can use the first-class lists.

I understood this sentence only because I’ve seen lambda calculus before. I would instead say “The Y combinator works with functions of one input. If you want to make a method with more than one input, one solution is to put them all in a list and break them out inside the function.” (I say “one solution” because another solution is currying.)

Now we can code using the similar thing for a Y-combinator to create it.

What? Do you mean, “we can code using the script form of the Y combinator to create it”? I’m truly not sure I’m getting this.

The full definition is now looks like the photo in Figure 2-2:

redundant “is”

Press OK,

You haven’t explicitly said that the user is constructing this code in the Block Editor. In fact, that’s not what your readers do, because we have smart script pics, so your reader will just drag the picture into a Snap! window.

In Chapter 3, I hope that isn’t really your name…

A better name for “Reset” is probably “set”, as it takes in a value to set the counter to. Reset would make sense if it always set it to zero (or other starting value) which it doesnt.

I fixed the contents.