RosettaCodeData/Task/Range-expansion/Icon/range-expansion.icon

26 lines
707 B
Plaintext

procedure main()
s := "-6,-3--1,3-5,7-11,14,15,17-20"
write("Input string := ",s)
write("Expanded list := ", list2string(range_expand(s)) | "FAILED")
end
procedure range_expand(s) #: return list of integers extracted from an ordered string representation
local R,low,high
R := []
s ? until pos(0) do {
put(R,low := integer(tab(upto(',-')|0))| fail) # get lower bound
if ="-" || (high := integer(tab(find(",")|0))|fail) then
until low = high do put(R,low +:= 1) # find range
=","
}
return R
end
procedure list2string(L) #: helper function to convert a list to a string
local s
every (s := "[ ") ||:= !L || " "
return s || "]"
end