RosettaCodeData/Task/Range-extraction/Bracmat/range-extraction.bracmat

52 lines
1.3 KiB
Plaintext

( rangeExtract
= accumulator firstInRange nextInRange
, accumulate fasten rangePattern
. ( accumulate
= !accumulator
(!accumulator:|?&",")
!firstInRange
( !firstInRange+1:<>!nextInRange
& ( !firstInRange+2:!nextInRange&","
| "-"
)
-1+!nextInRange
|
)
: ?accumulator
)
& ( fasten
= [%( !accumulate
& (!sjt:?firstInRange)+1:?nextInRange
)
)
& ( rangePattern
= (
| ?
( !nextInRange
& 1+!nextInRange:?nextInRange
)
)
( &!accumulate
| (#<>!nextInRange:!fasten) !rangePattern
)
)
& :?accumulator:?firstInRange
& !arg:(|#!fasten !rangePattern)
& str$!accumulator
)
& ( test
= L A
. put$(!arg " ==>\n",LIN)
& ( !arg:(?,?)
& whl'(!arg:(?A,?arg)&(!A,!L):?L)
& whl'(!L:(?A,?L)&!A !arg:?arg)
|
)
& out$(rangeExtract$!arg)
)
& test
$ (0, 1, 2, 4, 6, 7, 8, 11, 12, 14,
15, 16, 17, 18, 19, 20, 21, 22, 23, 24,
25, 27, 28, 29, 30, 31, 32, 33, 35, 36,
37, 38, 39)