Skip to content

Instantly share code, notes, and snippets.

@tonyonodi
Created November 26, 2016 13:29
Show Gist options
  • Save tonyonodi/31357be88c33e6c1599d4c220157a15c to your computer and use it in GitHub Desktop.
Save tonyonodi/31357be88c33e6c1599d4c220157a15c to your computer and use it in GitHub Desktop.
// This is my solution to the "Little JavaScript Problem" from http://lisperator.net/blog/a-little-javascript-problem/
// Apparently this means Mihai Bazon will hire me... Woo!
var pair = (head, tail) => (f => f(head, tail))
var car = (pair) => pair((head, tail) => head)
var cdr = (pair) => pair((head, tail) => tail)
var range = (min, max) => pair(min, min == max ? null : range(min + 1, max))
var map = (list, func) => pair(func(car(list)), cdr(list) === null ? null : map(cdr(list), func))
var reverse = (list, newList=null) => list === null ? newList : reverse(cdr(list),pair(car(list), newList))
var foreach = (list, func) => list === null ? null : (func(car(list)), foreach(cdr(list), func))
var numbers = range(1, 10);
numbers = map(numbers, function (n) { return n * n });
numbers = reverse(numbers);
foreach(numbers, console.log);
/* output:
100
81
64
49
36
25
16
9
4
1
*/
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment