Skip to content

Instantly share code, notes, and snippets.

View dc-mak's full-sized avatar

Dhruv Makwana dc-mak

  • Cambridge, UK
View GitHub Profile
@dc-mak
dc-mak / for_loops.re
Created July 7, 2018 19:26
For loops and accumulating arguments (Solving Problems in Computer Science, ATypcial CompSci)
/* 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:
--------------------------------------------------
@dc-mak
dc-mak / higher_order.re
Created July 7, 2018 19:25
Higher Order Functions (Solving Problems in Computer Science, ATypical CompSci)
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 */
@dc-mak
dc-mak / recursive_debug.re
Created July 7, 2018 19:24
Recursive Functions and Debugging (Solving Problems in Computer Science, ATypical CompSci)
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);
*/
@dc-mak
dc-mak / perf_common.ml
Last active January 11, 2018 22:00
Owl Perf Common (printing only times)
(* 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