[Jprogramming] Function creation, without creating subfunctions
Michael Rice
limitcase at gmail.com
Sun May 28 02:28:09 UTC 2017
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
More information about the Programming
mailing list