lisp.js
Version:
A tiny lisp interpreter
34 lines (25 loc) • 661 B
JavaScript
var cps_prim = function(f) {
return function() {
var args = Array.prototype.slice.apply(arguments).reverse();
return args[0](f.apply(null, args.slice(1).reverse()));
};
};
var add = cps_prim(function(x, y) {
return x + y;
});
add(1, 2, function(x) {console.log(x);});
var mul = cps_prim(function(x, y) {
return x * y;
});
mul(2, 2, function(x) {console.log(x);});
var my_sqrt = cps_prim(function(x) {
return Math.sqrt(x);
});
my_sqrt(2, function(x) {console.log(x);});
var pyth = function(x, y) {
return Math.sqrt(x*x + y*y);
};
var cps_pyth = cps_prim(pyth);
cps_pyth(3, 4, function(r) {
console.log(r);
});