Lexical variables and fry
Factor handbook > The language > Lexical variables

Prev:Mutable lexical variables
Next:Limitations of lexical variables


Lexical variables integrate with Fried quotations so that mixing variables with fried quotations gives intuitive results.

The following two code snippets are equivalent:
'[ sq _ + ]

[ [ sq ] dip + ] curry

The semantics of dip and curry are such that the first example behaves as if the top of the stack as “inserted” in the “hole” in the quotation's second element.

Conceptually, curry is defined so that the following two code snippets are equivalent:
3 [ - ] curry

[ 3 - ]

When quotations take named parameters using [|, curry fills in the variable bindings from right to left. The following two snippets are equivalent:
3 [| a b | a b - ] curry

[| a | a 3 - ]

Because of this, the behavior of fry changes when applied to such a quotation to ensure that fry conceptually behaves the same as with normal quotations, placing the fried values “underneath” the variable bindings. Thus, the following snippets are no longer equivalent:
'[ [| a | _ a - ] ]

'[ [| a | a - ] curry ] call

Instead, the first line above expands into something like the following:
[ [ swap [| a | a - ] ] curry call ]


The precise behavior is as follows. When frying a [| quotation, a stack shuffle (mnswap) is prepended so that the m curried values, which start off at the top of the stack, are transposed with the quotation's n named input bindings.