Normally, a fork(also known as a trident) is an isolated series of 3 functions(functions placed one after the other, known as a train), when arranged in this manner they have an invisible combinator applied to them such that if f, g and h are functions and x, y are arguments:
(f g h) y = f(y) g h(y)
x (f g h) y = (x f(y)) g (x h(y))
A hook(bident) is an isolated series of 2 functions, this also has an invisible combinator applied to it, such that if f and g are functions and x,y are arguments: (g h) y = y g h(y)
x (g h) y = x g h y
The motivation for using such constructs:1) It eliminates explicit variables, a lot of useful expressions can be simply written as a series of functions which are converted into hooks and forks by the above mechanism.
mean=: +/ % #2) A series of functions can now have dyadic functions embedded inside it(e.g. the central element of a fork). This allows us to use infix functions naturally and frees us from the limitations of a composition-only function application style, a limitation such as being unable to use infix functions in a series of functions without declaring explicit variables. This added expressibility is the reason for 1).
3) It gives utility to otherwise useless but syntactically valid expressions(with minimal additions to the parser). An isolated odd number of functions arranged in a series becomes a fork, an even number of functions becomes a hook.
diff =: - +/ % #
diff2=: - mean
(diff i.5) = diff2 i.5 NB. Equivalent
1
(diff = diff2) i.5 NB. A fork equivalent to the above
1
If the left element of a fork is a noun, then the fork behaves differently, the left noun is used as a left argument to the dyadic function g(the central element of the fork): (n g h) y = n g h(y)
x (n g h) y = n g (x h(y))
This is equivalent to currying g using n and is often used in the same manner, for example the expression 3n+1 can be written as a fork using this extension:1+3*]The special behaviour exhibited by forks with a left noun element is termed a "Left-leaning fork".
I would like to see a similar extension applied to hooks, a "Left-leaning hook", for the case of a noun followed by a verb. This was inspired by Nial which has a similar syntax for currying. Hooks can then be used to directly curry, no longer needing the conjunction &(bond).
(n g) y = n g y
x (n g) y = n g y
For example: f1 =: (3*)`(_5+~)@.*
collatz=: -:`(1+3*])@.(2|)
f2 =: 3 (4 : 'x%+:>:x+y') NB. Explicit definition curry
The motivation for this:1) It simplifies code. No longer needing explicit currying achieved using a conjunction, the purity of hooks and forks is maintained. This is analogous to how cap([:) gives forks the ability to perform function composition, in the same vein we are able to extend hooks to perform currying.
2) It is a logical extension to a currently unused syntactic expression.
(2+) = (2+])The implementation of this extension is trivial; the hook will simply check the first element's type, if it is a noun, a left-leaning hook is called in the manner specified above. The current parse table doesn't need to be changed because the line matching hooks already allows nouns on the left side. I have experimented with this in the old j7 source code.
No comments:
Post a Comment