SAPF, dreaded Y operator in forms and more

There seems to be some hidden rules for forms, particularly as concerns the (dreaded) Y operator that is perplexing to me.

For example, this does not work:

{
  :factorial \x f [x 2 < \[1] \[ x x -- f *] if] Y ; from Y help
} = foo

I have managed to figure out independently that all functions in forms want a final argument (usually put as ‘o’, object?), but this also doesn’t work:

{
  :factorial \x f o[x 2 < \[1] \[ x x -- f *] if] Y
} = foo

So, what’s the deal here?

Also:

How do you refer to a value in a form when assigning a value to another key in general.

For example:

{
  :x 42
  :y \o[o.x]
} = foo

foo.y is 42

{
  :x 42
  :y o.x
} = foo

gives:

“o” is an undefined word, when compiling.

Anybody have any ideas?

              Thank You For Your Attention

The Y function’s name comes from the Y combinator in computer science. It takes a function that takes a function as an input and returns a function that calls the inner function with itself as the last argument.

The o argument is the convention in sapf for representing this or self. So when you do foo.y, foo is passed as the argument to the function and is bound to function argument variable o. Then o.y returns the value of expression o.x. In your second foo example, o is not defined. You cannot refer to another field of an object within its own definition because the object has not been created yet. But this works:

{ 42 aa :x :y } = foo
 foo.y pr cr 
--> 42
3 Likes