Skip to content

Instantly share code, notes, and snippets.

@chad3814
Forked from sjl/list-out-of-lambda.js
Last active October 22, 2021 20:15
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save chad3814/10416328 to your computer and use it in GitHub Desktop.
Save chad3814/10416328 to your computer and use it in GitHub Desktop.
fixed jslint errors, spacing a bit
'use strict';
var not = function (x) {
if (x) {
return false;
}
return true;
};
var and = function (a, b) {
if (a) {
if (b) {
return true;
}
return false;
}
return false;
};
var or = function (a, b) {
if (a) {
return true;
}
if (b) {
return true;
}
return false;
};
'use strict';
/*global is_zero, inc, dec, sub, zero*/
var empty_list = function (selector) {
return selector(undefined, undefined, true);
};
var prepend = function (el, list) {
return function (selector) {
return selector(el, list, false);
};
};
var head = function (list) {
return list(function (h, t, e) {
return h;
});
};
var tail = function (list) {
return list(function (h, t, e) {
return t;
});
};
var is_empty = function (list) {
return list(function (h, t, e) {
return e;
});
};
var map = function (fn, l) {
if (is_empty(l)) {
return empty_list;
}
return prepend(fn(head(l)), map(fn, tail(l)));
};
var filter = function (fn, l) {
if (is_empty(l)) {
return empty_list;
}
if (fn(head(l))) {
return prepend(head(l), filter(fn, tail(l)));
}
return filter(fn, tail(l));
};
var nth = function (l, n) {
if (is_zero(n)) {
return head(l);
}
return nth(tail(l), dec(n));
};
var drop = function (l, n) {
if (is_zero(n)) {
return l;
}
return drop(tail(l), dec(n));
};
var take = function (l, n) {
if (is_zero(n)) {
return empty_list;
}
return prepend(head(l), take(tail(l), dec(n)));
};
var slice = function (l, start, end) {
return take(drop(l, start), sub(end, start));
};
var length = function (l) {
if (is_empty(l)) {
return zero;
}
return inc(length(tail(l)));
};
'use strict';
/*global empty_list, prepend, head, tail, is_empty, and, or, not*/
var zero = empty_list;
var inc = function (n) {
return prepend(empty_list, n);
};
var dec = function (n) {
return tail(n);
};
var one = inc(zero);
var is_zero = function (n) {
return is_empty(n);
};
var add = function (a, b) {
if (is_zero(b)) {
return a;
}
return add(inc(a), dec(b));
};
var sub = function (a, b) {
if (is_zero(b)) {
return a;
}
return add(dec(a), dec(b));
};
var mul = function (a, b) {
if (is_zero(b)) {
return zero;
}
return add(a, mul(a, dec(b)));
};
var pow = function (a, b) {
if (is_zero(b)) {
return one;
}
return mul(a, pow(a, dec(b)));
};
var is_equal = function (n, m) {
if (and(is_zero(n), is_zero(m))) {
return true;
}
if (or(is_zero(n), is_zero(m))) {
return false;
}
return is_equal(dec(n), dec(m));
};
var less_than = function (a, b) {
if (and(is_zero(a), is_zero(b))) {
return false;
}
if (is_zero(a)) {
return true;
}
if (is_zero(b)) {
return false;
}
return less_than(dec(a), dec(b));
};
var greater_than = function (a, b) {
return less_than(b, a);
};
var div = function (a, b) {
if (less_than(a, b)) {
return zero;
}
return inc(div(sub(a, b), b));
};
var rem = function (a, b) {
if (less_than(a, b)) {
return a;
}
return rem(sub(a, b), b);
};
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment