Created
January 29, 2019 14:25
-
-
Save bluurn/8c65b8debfc54fc49d3b2abd225a7f2b to your computer and use it in GitHub Desktop.
JS: Pair Implementation
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
var cons = function (x, y) { | |
return function(message) { | |
switch (message) { | |
case 'car': { | |
return x; | |
} | |
case 'cdr': { | |
return y; | |
} | |
} | |
}; | |
}; | |
var car = function(pair) { | |
return pair('car'); | |
}; | |
var cdr = function(pair) { | |
return pair('cdr'); | |
} | |
var pair = cons('a', 'b'); | |
console.assert(car(pair) == 'a'); | |
console.assert(cdr(pair) == 'b'); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment