Created
June 25, 2015 01:07
-
-
Save theqabalist/b1c16e9ce393c8befb55 to your computer and use it in GitHub Desktop.
Lisp.js
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
(function () { | |
"use strict"; | |
var pair = (function () { | |
function cons(x, y) { | |
return function (pick) { | |
return pick ? x : y; | |
}; | |
} | |
function car(pair) { | |
return pair(true); | |
} | |
function cdr(pair) { | |
return pair(false); | |
} | |
return { | |
cons: cons, | |
car: car, | |
cdr: cdr | |
}; | |
}()), | |
arr = (function () { | |
function length(a) { | |
return a.length; | |
} | |
function head(a) { | |
return a[0]; | |
} | |
function tail(a) { | |
return a.slice(1); | |
} | |
function empty(a) { | |
return a.length === 0; | |
} | |
function concat(a, item) { | |
return a.concat(item); | |
} | |
function join(delim, a) { | |
return a.join(delim); | |
} | |
return { | |
concat: concat, | |
empty: empty, | |
length: length, | |
head: head, | |
tail: tail, | |
join: join | |
}; | |
}()), | |
fargs = (function () { | |
function toArray(args) { | |
var newArr = [], | |
i = 0, | |
len = args.length; | |
while (i < len) { | |
newArr.push(args[i]); | |
i += 1; | |
} | |
return newArr; | |
} | |
return { | |
toArray: toArray | |
}; | |
}()), | |
func = (function () { | |
function apply(f, args) { | |
return f.apply(null, args); | |
} | |
return { | |
apply: apply | |
}; | |
}()), | |
str = (function () { | |
function build() { | |
return arr.join("", fargs.toArray(arguments)); | |
} | |
return { | |
build: build | |
}; | |
}()), | |
list = (function () { | |
function length(list) { | |
function _length(current, list) { | |
var head = pair.car(list); | |
return head ? _length(current + 1, pair.cdr(list)) : current; | |
} | |
return _length(0, list); | |
} | |
function empty(list) { | |
return length(list) === 0; | |
} | |
function makeList() { | |
var args = fargs.toArray(arguments); | |
return arr.empty(args) ? | |
pair.cons(null, null) : | |
pair.cons(arr.head(args), func.apply(makeList, arr.tail(args))); | |
} | |
function reduce(f, a, l) { | |
return empty(l) ? a : reduce(f, f(a, pair.car(l)), pair.cdr(l)); | |
} | |
function concat(l1, l2) { | |
return empty(l1) ? l2 : pair.cons(pair.car(l1), concat(pair.cdr(l1), l2)); | |
} | |
function map(f, l) { | |
return reduce(function (newList, item) { | |
return concat(newList, makeList(f(item))); | |
}, makeList(), l); | |
} | |
function toArray(l) { | |
return reduce(function (acc, item) { | |
return arr.concat(acc, item); | |
}, [], l); | |
} | |
function display(l) { | |
return str.build("(", arr.join(" ", toArray(l)), ")"); | |
} | |
return { | |
length: length, | |
makeList: makeList, | |
reduce: reduce, | |
map: map, | |
concat: concat, | |
display: display | |
}; | |
}()), | |
myList = list.makeList(1, 2, 3, 4); | |
console.log(pair.car(pair.cdr(pair.cdr(pair.cdr(myList))))); | |
console.log(list.length(myList)); | |
console.log(list.length(list.makeList())); | |
console.log(list.reduce(function (x, y) { return x + y; }, 0, myList)); | |
console.log(list.display(list.concat(myList, list.makeList(5)))); | |
console.log(list.display(list.map(function (x) { return x * 10; }, myList))); | |
}()); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment