Created
February 21, 2022 22:40
-
-
Save pema99/935b915a3197b5222183bf6ac4bb8308 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
// Input program | |
sum List 'a = | |
| Cons 'a * List 'a | |
| Nil unit | |
let iota = [n] | |
let inner = rec [inner] [acc] | |
if acc >= n then Nil () | |
else Cons (acc, inner (acc + 1)) | |
in | |
inner 0 | |
in | |
let rec map = [f] [lst] | |
match lst with | |
| Cons (h, t) -> Cons (f h, map f t) | |
| Nil _ -> Nil () | |
in | |
let rec fold = [f] [z] [lst] | |
match lst with | |
| Cons (h, t) -> f (h) (fold f z t) | |
| Nil _ -> z | |
in | |
let filter = rec [filter] [f] [lst] | |
match lst with | |
| Cons (h, t) -> | |
if f h | |
then Cons (h, filter f t) | |
else filter f t | |
| Nil _ -> Nil _ | |
in | |
let myList = iota 20 in | |
let r1 = map ([x] x * x) myList in | |
let r2 = filter ([x] x % 2 = 0) r1 in | |
let r3 = fold (+) 0 r2 in | |
r3 |
This file contains hidden or 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
// Output JS | |
var Cons = function (v) { | |
return { tag: "Cons", val: v }; | |
}; | |
var Nil = function (v) { | |
return { tag: "Nil", val: v }; | |
}; | |
(function() { | |
var iota = function (n) { | |
var inner = function (acc) { | |
if ((acc >= n)) { | |
return Nil("<unit>"); | |
} else { | |
return Cons([acc, inner((acc + 1))]); | |
} | |
}; | |
return inner(0); | |
}; | |
var map = function (f) { | |
return function (lst) { | |
var h = null; | |
var t = null; | |
var _ = null; | |
var matched = 0; | |
if (((lst).tag === "Cons")) { | |
h = ((lst).val)[0]; | |
t = ((lst).val)[1]; | |
matched = 0; | |
} | |
if (((lst).tag === "Nil")) { | |
_ = (lst).val; | |
matched = 1; | |
} | |
switch (matched) { | |
case 0: { | |
return Cons([f(h), map(f)(t)]); | |
} break; | |
case 1: { | |
return Nil("<unit>"); | |
} break; | |
} | |
}; | |
}; | |
var fold = function (f) { | |
return function (z) { | |
return function (lst) { | |
var h = null; | |
var t = null; | |
var _ = null; | |
var matched = 0; | |
if (((lst).tag === "Cons")) { | |
h = ((lst).val)[0]; | |
t = ((lst).val)[1]; | |
matched = 0; | |
} | |
if (((lst).tag === "Nil")) { | |
_ = (lst).val; | |
matched = 1; | |
} | |
switch (matched) { | |
case 0: { | |
return f(h)(fold(f)(z)(t)); | |
} break; | |
case 1: { | |
return z; | |
} break; | |
} | |
}; | |
}; | |
}; | |
var filter = function (f) { | |
return function (lst) { | |
var h = null; | |
var t = null; | |
var _ = null; | |
var matched = 0; | |
if (((lst).tag === "Cons")) { | |
h = ((lst).val)[0]; | |
t = ((lst).val)[1]; | |
matched = 0; | |
} | |
if (((lst).tag === "Nil")) { | |
_ = (lst).val; | |
matched = 1; | |
} | |
switch (matched) { | |
case 0: { | |
if (f(h)) { | |
return Cons([h, filter(f)(t)]); | |
} else { | |
return filter(f)(t); | |
} | |
} break; | |
case 1: { | |
return Nil(_); | |
} break; | |
} | |
}; | |
}; | |
var myList = iota(20); | |
var r1 = map(function (x) { | |
return (x * x); | |
})(myList); | |
var r2 = filter(function (x) { | |
return ((x % 2) === 0); | |
})(r1); | |
var r3 = fold(function (x) { | |
return function (y) { | |
return (x + y); | |
}; | |
})(0)(r2); | |
return r3; | |
})(); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment