Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
40
Task/Range-extraction/Mercury/range-extraction.mercury
Normal file
40
Task/Range-extraction/Mercury/range-extraction.mercury
Normal file
|
|
@ -0,0 +1,40 @@
|
|||
:- module range_extraction.
|
||||
:- interface.
|
||||
|
||||
:- import_module io.
|
||||
|
||||
:- pred main(io::di, io::uo) is det.
|
||||
|
||||
:- implementation.
|
||||
|
||||
:- import_module int, list, ranges, string.
|
||||
|
||||
main(!IO) :-
|
||||
print_ranges(numbers, !IO).
|
||||
|
||||
:- pred print_ranges(list(int)::in, io::di, io::uo) is det.
|
||||
|
||||
print_ranges(Nums, !IO) :-
|
||||
Ranges = ranges.from_list(Nums),
|
||||
ranges.range_foldr(add_range_string, Ranges, [], RangeStrs),
|
||||
io.write_list(RangeStrs, ",", io.write_string, !IO).
|
||||
|
||||
:- pred add_range_string(int::in, int::in,
|
||||
list(string)::in, list(string)::out) is det.
|
||||
|
||||
add_range_string(L, H, !Strs) :-
|
||||
( if L = H then
|
||||
!:Strs = [int_to_string(L) | !.Strs]
|
||||
else if L + 1 = H then
|
||||
!:Strs = [int_to_string(L), int_to_string(H) | !.Strs]
|
||||
else
|
||||
!:Strs = [string.format("%d-%d", [i(L), i(H)]) | !.Strs]
|
||||
).
|
||||
|
||||
:- func numbers = list(int).
|
||||
|
||||
numbers = [
|
||||
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].
|
||||
Loading…
Add table
Add a link
Reference in a new issue