ocaml: phase 5.1 pascal.ml baseline (Pascal triangle row 10 middle = C(10,5) = 252)
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
Some checks failed
Test, Build, and Deploy / test-build-deploy (push) Failing after 27s
next_row prepends 1, walks adjacent pairs (x, y) emitting x+y,
appends a final 1:
let rec next_row prev =
let rec aux a =
match a with
| [_] -> [1]
| x :: y :: rest -> (x + y) :: aux (y :: rest)
| [] -> []
in
1 :: aux prev
row n iterates next_row n times starting from [1] using a ref +
'for _ = 1 to n do r := next_row !r done'.
row 10 = [1;10;45;120;210;252;210;120;45;10;1]
List.nth (row 10) 5 = 252 = C(10, 5)
Exercises three-arm match including [_] singleton wildcard, x :: y
:: rest binding, and the for-loop with wildcard counter. 45 baseline
programs total.
This commit is contained in:
@@ -29,6 +29,7 @@
|
||||
"newton_sqrt.ml": 1414,
|
||||
"mutable_record.ml": 10,
|
||||
"option_match.ml": 5,
|
||||
"pascal.ml": 252,
|
||||
"pi_leibniz.ml": 314,
|
||||
"pretty_table.ml": 64,
|
||||
"poly_stack.ml": 5,
|
||||
|
||||
19
lib/ocaml/baseline/pascal.ml
Normal file
19
lib/ocaml/baseline/pascal.ml
Normal file
@@ -0,0 +1,19 @@
|
||||
let rec next_row prev =
|
||||
let rec aux a =
|
||||
match a with
|
||||
| [_] -> [1]
|
||||
| x :: y :: rest -> (x + y) :: aux (y :: rest)
|
||||
| [] -> []
|
||||
in
|
||||
1 :: aux prev
|
||||
|
||||
let row n =
|
||||
let r = ref [1] in
|
||||
for _ = 1 to n do
|
||||
r := next_row !r
|
||||
done;
|
||||
!r
|
||||
|
||||
;;
|
||||
|
||||
List.nth (row 10) 5
|
||||
Reference in New Issue
Block a user