Skip to content

Instantly share code, notes, and snippets.

@jfmengels
Last active Jan 24, 2022
Embed
What would you like to do?
TRMC in Elm
/* Corresponds to the following Elm code
map fn list =
case list of
[] -> []
x :: xs ->
fn x :: map fn xs
*/
var map = function (fn, list) {
if (!list.b) {
return _List_Nil;
} else {
var x = list.a;
var xs = list.b;
return cons(
fn(x),
map(fn, xs)
);
}
};
var map = function (fn, list) {
var $start = _List_Cons(undefined, _List_Nil);
var $end = $start;
map: while (true) {
if (!list.b) {
return $start.b;
} else {
var x = list.a;
var xs = list.b;
$end = $end.b = _List_Cons(fn(x), _List_Nil);
list = xs;
continue map;
}
}
}
// For comparison, this is a plain tail recursive function (this function is useless though)
// Elm
foo : List a -> List b
foo list =
case list of
[] ->
[]
_ :: xs ->
foo xs
// JavaScript
var foo = function (list) {
foo: while (true) {
if (!list.b) {
return _List_Nil;
} else {
var xs = list.b;
list = xs;
continue foo;
}
}
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment