[Jprogramming] Function creation, without creating subfunctions
Raul Miller
rauldmiller at gmail.com
Sun May 28 02:39:03 UTC 2017
Actually, thinking about this further, the parenthesis around f are
not actually needed. But that is because f is a fork, and the default
behavior will be to construct a fork before incorporating the
definition of g. (If f was a hook, you would still need those parens.
The interpreter does not eliminate quite all unnecessary parenthesis
when representing verbs as their linear definitions - a few extra may
be included for cases where it seems likely that that will enhance
readability.)
Thanks,
--
Raul
On Sat, May 27, 2017 at 10:35 PM, Raul Miller <rauldmiller at gmail.com> wrote:
> Basically, you put the definition of f in parenthesis (though it
> already is, so no actual change needed there), and the definition of g
> in parenthesis.
>
> You can even get the interpreter to do this for you:
>
> 9!:3]5
>
> make_move
> g f
> make_move f.
> ((+. *. -.@:*.) +./) (1 0 0 0 0 0 0 0 0 0 0 0 0 0 0 |.!.0~ 3 1 $ -)
>
> Though, I would be tempted to replace 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
> with (15{.1) in a definition.
>
> I hope this helps,
>
> --
> Raul
>
>
> On Sat, May 27, 2017 at 10:28 PM, Michael Rice <limitcase at gmail.com> wrote:
>> NB. 15 position peg solitaire
>> NB. The board (position 5 initially vacant)
>>
>> NB. 0
>> NB. 1 2
>> NB. 3 4 5
>> NB. 6 7 8 9
>> NB. 10 11 12 13 14
>>
>> xor =: ((+.)*.(-.@:*.))
>> board =: 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1 NB. 0s for vacant, 1s occupied
>> board
>> 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1
>> move =: 0 2 5
>> move
>> 0 2 5
>> board xor +./((1 0 0 0 0 0 0 0 0 0 0 0 0 0 0) |.!.0~ 3 1 $ -) move
>> 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 NB. The desired result, a new board
>> f =: ((1 0 0 0 0 0 0 0 0 0 0 0 0 0 0) |.!.0~ 3 1 $ -)
>> f move
>> 1 0 0 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 1 0 0 0 0 0 0 0 0 0 0 0 0
>> 0 0 0 0 0 1 0 0 0 0 0 0 0 0 0
>> +./f move
>> 1 0 1 0 0 1 0 0 0 0 0 0 0 0 0 NB. Move 0 2 5 expressed as 15 element
>> binary array
>> board xor +./f move
>> 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 NB. Again, the desired result
>> board NB. Both board and move unchanged
>> 1 1 1 1 1 0 1 1 1 1 1 1 1 1 1
>> move
>> 0 2 5
>> g =: xor +./
>> make_move =: g f
>> board make_move move
>> 0 1 0 1 1 1 1 1 1 1 1 1 1 1 1 NB. Again, the desired result
>>
>> ++++++++++++++++++++++++++++++++++
>>
>> The question: How to write make_move without creating subfunctions f and g?
>>
>> I've tried several definition variations and keep getting 'length error'
>> messages upon usage.
>>
>> One example:
>> make_move =: xor+./((1 0 0 0 0 0 0 0 0 0 0 0 0 0 0) |.!.0~ 3 1 $ -)
>> board make_move move
>> |length error: make_move
>> ----------------------------------------------------------------------
>> For information about J forums see http://www.jsoftware.com/forums.htm
More information about the Programming
mailing list