User:Dan Bron/Snippets/AlternatingValences
Given the capped fork behavor reported by Henry Rich, we can create a verb which alternates between a fork and a capped fork, on every invocation.
That is, we can have a fork whose middle tine is invoked monadically or dyadically, depending on the number of times the train's been invoked before (instead of on the number of arguments to the train) :
NB. See Henry Rich's msg at NB. http://www.jsoftware.com/pipermail/programming/2006-May/002349.html left =: [: middle =: 3 : ('left =: [:`[@.A_z_';'y') @: mrd right =: 3 : 'A_z_ =: -.{:0,".''A_z_'' ' :($:@:]) NB. Returns 'monad' iff middle tine was called monadically NB. and 'dyad' iff middle tine was called dyadically. mrd =: 'monad'"_ :('dyad'"_) f =: left middle right f 0 monad f 0 dyad f 0 monad f~ 0 dyad f~ 0 monad f~ 0 dyad f"0 i.3 monad dyad monad f~"0 i.3 dyad monad dyad (left middle right) 0 monad (left middle right) 0 dyad
This trick was inspired by B Jonas on the J IRC channels .
Can you find other ways to acheive the same end? You are permitted to use side effects, like above, but you can't use cheap tricks like having the monad invoke the dyad (or not), based on some flag. The first and only valence invoked must change at every execution.
Attempting another line of attack, I found some surprising behavior:
g =: 3 : ('g =: 0&g';' ''monad'' ') : (4 : ('g =: g@:]';' ''dyad'' ')) g 3 : 0 :(4 : 0) g =: g@:] 'dyad' ) g =: 0&g 'monad' ) g 0 monad g 0 0 g 0 0 g 3 3 g~ 4 4 3 g 17 17 3 g 'x' x 'x' g 3 |domain error: g | 'x' g 3 NB. Aha! Powering function of & NB. Since g y is 0&g y is 0 g y is 0 (0&g) y NB. and we have x m&u y <--> m&u^:x y then NB. g y <--> 0&g^:0 y . And, and by definition NB. u^:0 y is simply y . 'g' f. 0&(0&g)
Is the above (i.e. that g f. isn't the same as 'g' f.) a bug?
There has to be an way to present this as an interesting challenge on the Forums (like <"0 and g^:_1) (link to these).
For example:
what is the definition of g such that g f. 2 : ' ''u'' -:&:(5!:2)@:;: ''v'' ' 'g' f.?
Or maybe find a way to phrase:
find an adverb which turns any verb into an identity function
But this one could be more than just (0&)~. For example, ([`) (]`) (`:6))).
Perhaps:
find a verb which is the identity function when assigned to a name, but an error when not assigned to a name
Is there anything besides u=:0&u~ ?