RosettaCodeData/Task/Range-expansion/TXR/range-expansion.txr
2023-07-01 13:44:08 -04:00

43 lines
1,012 B
Text

@(define num (n))@(local tok)@{tok /[+\-]?\d+/}@(bind n @(int-str tok))@(end)
@(define entry (e))@\
@(local n1 n2)@\
@(cases)@\
@(num n1)@/\s*-\s*/@(num n2)@\
@(bind e (n1 n2))@\
@(or)@\
@(num n1)@\
@(bind e n1)@\
@(end)@\
@(end)
@(define rangelist (list))@\
@(local first rest)@\
@(cases)@\
@(entry first)@/\s*,\s*/@(rangelist rest)@\
@(bind list @(cons first rest))@\
@(or)@\
@(entry first)@\
@(bind list (first))@\
@(or)@\
@(bind list nil)@\
@(end)@\
@(end)
@(do
(defun expand-helper (list)
(cond
((null list) nil)
((consp (first list))
(append (range (first (first list))
(second (first list)))
(rangeexpand (rest list))))
(t (cons (first list) (rangeexpand (rest list))))))
(defun rangeexpand (list)
(uniq (expand-helper list))))
@(repeat)
@(rangelist x)@{trailing-junk}
@(output)
raw syntax: @x
expansion: @(rangeexpand x)
your junk: @{trailing-junk}
@(end)
@(end)