This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
/* Imperative for-loop for factorial: | |
--------------------------------- | |
int factorial(int n) | |
int result = 1 | |
for (int i = 2; i <= n; i = i + 1) | |
result = result * i | |
return result | |
*/ | |
/* Transformation of for-loop into recursive function: | |
-------------------------------------------------- |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let id = x => x; | |
let just = (y, _) => y; | |
let (+) = (f, g, x) => f(x) +. g(x); | |
let (-) = (f, g, x) => f(x) -. g(x); | |
let ( * ) = (f, g, x) => f(x) *. g(x); | |
let (@) = (f, g, x) => f(g(x)); | |
let sq = id * id; | |
let f = id + sin * (cos @ sq) - just(3.); | |
/* Outside Inwards */ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
let directSumFrom1To = (n: int): int => n * (n + 1) / 2; | |
let directSumFrom1To: int => int = n => n * (n + 1) / 2; | |
let directSumFrom1To = n => n * (n + 1) / 2; | |
/* Step 1 */ | |
/* | |
* let choose_from = (k, n) => | |
* choose_from(k - 1, n - 1) + choose_from(k, n - 1); | |
*/ |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
(* helper functions for performance test *) | |
(* test one operation c times, output the mean time *) | |
let test_op s c op = | |
let ttime = ref 0. in | |
for i = 1 to c do | |
Gc.compact (); | |
let t0 = Unix.gettimeofday () in | |
let _ = op () in | |
let t1 = Unix.gettimeofday () in |