-
-
Save jfmengels/37241eb3fe20a8f37d36891249703e70 to your computer and use it in GitHub Desktop.
TRMC in Elm
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
/* 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