Last active
August 29, 2015 14:07
-
-
Save andresvia/dbc3c18322540d5bff04 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
var reverseArray = function(a) { | |
var r = [] | |
for(var i = a.length-1; i>=0 ; i--) { | |
r.push(a[i]) | |
} | |
return r | |
} | |
var prepend = function (v, l) { | |
return {value:v, rest:l} | |
} | |
var arrayToList = function(a) { | |
var l = null | |
var r = reverseArray(a) | |
for (var i = 0; i < r.length ; i++) { | |
l = prepend(r[i], l) | |
} | |
return l | |
} | |
var listToArray = function (l) { | |
var a = [] | |
for(var n = l ; n ; n = n.rest) { | |
a.push(n.value) | |
} | |
return a | |
} | |
var nth = function (l, i) { | |
if (l == null) return undefined | |
var loop = function(l, i, c) { | |
if (c == i && l != null) return l.value | |
else if (c < i) return loop(l.rest, i, c+1) | |
else return undefined | |
} | |
return loop(l, i, 0) | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment