Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 22s
Inverse of run_length.ml from iteration 130. Takes a list of
(value, count) tuples and expands:
let rec rle_decode pairs =
match pairs with
| [] -> []
| (x, n) :: rest ->
let rec rep k = if k = 0 then [] else x :: rep (k - 1) in
rep n @ rle_decode rest
rle_decode [(1,3); (2,2); (3,4); (1,2)]
= [1;1;1; 2;2; 3;3;3;3; 1;1]
sum = 3 + 4 + 12 + 2 = 21.
Tests tuple-cons pattern, inner-let recursion, list concat (@), and
the 'List.fold_left (+) 0' invariant on encoding round-trips.
81 baseline programs total.
11 lines
243 B
OCaml
11 lines
243 B
OCaml
let rec rle_decode pairs =
|
|
match pairs with
|
|
| [] -> []
|
|
| (x, n) :: rest ->
|
|
let rec rep k = if k = 0 then [] else x :: rep (k - 1) in
|
|
rep n @ rle_decode rest
|
|
|
|
;;
|
|
|
|
List.fold_left (+) 0 (rle_decode [(1, 3); (2, 2); (3, 4); (1, 2)])
|