Not a member of Pastebin yet?
Sign Up,
it unlocks many cool features!
- const TRUE = x => y => x;
- const FALSE = x => y => y;
- const AND = p => q => p(q)(p);
- const OR = p => q => p(p)(q);
- const NOT = p => p(FALSE)(TRUE);
- const XOR = p => q => p(p)(q)(p(q)(p)(FALSE)(TRUE))(p(p)(q));
- const EQBOOL = p => q => p(p)(q)(p(q)(p)(FALSE)(TRUE))(p(p)(q))(FALSE)(TRUE);
- const IFTHENELSE = p => a => b => p(a)(b);
- const ZERO = f => x => x;
- const SUCC = n => f => x => f(n(f)(x));
- const ADD = m => n => f => x => m(f)(n(f)(x));
- const MULT = m => n => f => m(n(f));
- const POW = b => e => e(b);
- const PRED = n => f => x => n(g => h => h(g(f)))(u => x)(u => u);
- const SUB = m => n => n(PRED)(m);
- const ISZERO = n => n(x => FALSE)(TRUE);
- const LEQ = m => n => ISZERO(SUB(m)(n));
- const GEQ = m => n => LEQ(n)(m);
- const EQ = m => n => AND(LEQ(m)(n))(LEQ(n)(m));
- const LT = m => n => NOT(GEQ(m)(n));
- const GT = m => n => NOT(LEQ(m)(n));
- const ONE = SUCC(ZERO);
- const TWO = SUCC(ONE);
- const THREE = SUCC(TWO);
- const FOUR = SUCC(THREE);
- const FIVE = SUCC(FOUR);
- const SIX = SUCC(FIVE);
- const SEVEN = SUCC(SIX);
- const EIGHT = SUCC(SEVEN);
- const NINE = SUCC(EIGHT);
- const TEN = SUCC(NINE);
- const ONEHUNDRED = POW(TEN)(TWO);
- const PAIR = x => y => f => f(x)(y);
- const FIRST = p => p(TRUE);
- const SECOND = p => p(FALSE);
- const CONS = h => t => c => n => c(h)(t(c)(n));
- const NIL = c => n => n;
- const ISNIL = l => l(h => t => FALSE)(TRUE);
- const HEAD = l => l(TRUE)(FALSE);
- const TAIL = l => c => n => l(h => t => g => g(h)(t(c)))(t => n)(h => t => t);
- const REDUCE = l => f => x => l(f)(x);
- //iterates over the list and applies a given function f of two arguments, where the first represents the current value in the list and the second represents the accumulator, and returns the accumulator, where the initial value of the accumulator is x
- const LENGTH = l => l(x => y => SUCC(y))(ZERO);
- const MAP = l => f => l(x => y => CONS(f(x))(y))(NIL);
- const FILTER = l => f => l(x => y => IFTHENELSE(f(x))(CONS(x)(y))(y))(NIL);
- const APPEND = l => x => l(x => y => CONS(x)(y))(CONS(x)(NIL));
- const INDEX = l => i => HEAD(i(TAIL)(l));
- const REVERSE = l => l(x => y => APPEND(y)(x))(NIL);
- // const FOOT = l => HEAD(REVERSE(l)); //last element of list
- const FOOT = l => HEAD(PRED(LENGTH(l))(TAIL)(l));
- const BODY = l => REVERSE(TAIL(REVERSE(l))); //list with last element removed
- const DROP = l => n => n(TAIL)(l); //removes the first n elements from the list
- const TAKE = l => n => REVERSE(l)(x => y => IFTHENELSE(LT(LENGTH(y))(n))(APPEND(y)(x))(y))(NIL); //gives first n elements of list
- const EXAMPLELIST = CONS(ONE)(CONS(TWO)(CONS(THREE)(CONS(FOUR)(CONS(FIVE)(NIL)))));
- const Y = f => (x => f(y => x(x)(y)))(x => f(y => x(x)(y))); //the Y-combinator (actually, this is the Z combinator because javascript doesn't have lazy evaluation, but they're eta-equivalent)
- const FACT_GEN = f => n => IFTHENELSE(ISZERO(n))(() => ONE)(() => MULT(n)(f(PRED(n))))();
- const FACT = Y(FACT_GEN);
- //because javascript lacks lazy evaluation, ifthenelse must be adjusted slightly when defining recursive generator functions i.e. instead of IFTHENELSE(if)(then)(else) must have IFTHENELSE(if)(() => then)(() => else)()
- const FIB_GEN = f => n => IFTHENELSE(ISZERO(n))(() => ZERO)(() => IFTHENELSE(EQ(n)(ONE))(() => ONE)(() => ADD(f(PRED(n)))(f(PRED(PRED(n)))))())();
- const FIB = Y(FIB_GEN);
- const DIV_GEN = f => m => n => IFTHENELSE(ISZERO(n))(() => ZERO)(() => IFTHENELSE(LT(m)(n))(() => ZERO)(() => SUCC(f(SUB(m)(n))(n)))())();
- const DIV = Y(DIV_GEN); //euclidean integer division, defined recursively, where division by zero returns zero
- const MOD_GEN = f => m => n => IFTHENELSE(ISZERO(n))(() => ZERO)(() => IFTHENELSE(LT(m)(n))(() => m)(() => f(SUB(m)(n))(n))())();
- const MOD = Y(MOD_GEN);
- const GCD_GEN = f => m => n => IFTHENELSE(ISZERO(n))(() => m)(() => f(n)(MOD(m)(n)))();
- const GCD = Y(GCD_GEN);
- const ISEVEN = n => ISZERO(MOD(n)(TWO));
- const ISODD = n => NOT(ISEVEN(n));
- const DIVPOSINT_GEN = f => a => b => IFTHENELSE(LTINT(a)(b))(() => ZEROINT)(() => SUCCINT(f(SUBINT(a)(b))(b)))(); //euclidean division of positive integers
- const DIVPOSINT = Y(DIVPOSINT_GEN);
- const DIVINT_GEN = f => a => b => IFTHENELSE(ISZEROINT(b))(() => ZEROINT)(() => IFTHENELSE(ISNEG(b))(() => NEG(f(a)(NEG(b))))(() => IFTHENELSE(ISNEG(a))(() => NEG(f(NEG(a))(b)))(() => DIVPOSINT(a)(b))())())(); //euclidean division allowing for negative integers where (-a)//b = a//(-b) = -(a//b) and (-a)//(-b) = a//b for positive a and b
- const DIVINT = Y(DIVINT_GEN);
- const MODPOSINT_GEN = f => a => b => IFTHENELSE(LTINT(a)(b))(() => a)(() => f(SUBINT(a)(b))(b))(); //modulo for positive integers
- const MODPOSINT = Y(MODPOSINT_GEN);
- const MODINT_GEN = f => a => b => IFTHENELSE(ISZEROINT(b))(() => ZEROINT)(() => IFTHENELSE(ISNEG(b))(() => f(a)(NEG(b)))(() => IFTHENELSE(ISNEG(a))(() => NEG(f(NEG(a))(b)))(() => MODPOSINT(a)(b))())())(); //modulo allowing for negative numbers where a%(-b) = a%b and (-a)%b = (-a)%(-b) = -(a%b) for positive a and b
- const MODINT = Y(MODINT_GEN);
- const GCDINT_GEN = f => a => b => IFTHENELSE(ISZEROINT(b))(() => ABS(a))(() => f(b)(MODINT(a)(b)))();
- const GCDINT = Y(GCDINT_GEN);
- const ISEVENINT = n => ISZEROINT(MODINT(n)(TWOINT));
- const ISODDINT = n => NOT(ISEVENINT(n));
- const INF = n => n(x => APPEND(x)(SUCC(FOOT(x))))(CONS(ZERO)(NIL));
- const RANGE = m => n => DROP(INF(n))(m);
- const INT = n => PAIR(n)(ZERO); //converts a church natural to a church integer
- const NEG = n => n(a => b => PAIR(b)(a));
- const ADDINT = m => n => m(a => b => n(c => d => PAIR(ADD(a)(c))(ADD(b)(d))));
- const SUBINT = m => n => ADDINT(m)(NEG(n));
- const MULTINT = m => n => m(a => b => n(c => d => PAIR(ADD(MULT(a)(c))(MULT(b)(d)))(ADD(MULT(a)(d))(MULT(b)(c)))));
- const ISZEROINT = m => m(a => b => EQ(a)(b));
- const ISPOS = m => m(a => b => GT(a)(b));
- const ISNEG = m => m(a => b => LT(a)(b));
- const SUCCINT = m => IFTHENELSE(ISNEG(m))(m(a => b => PAIR(a)(PRED(b))))(m(a => b => PAIR(SUCC(a))(b)));
- const ABS = m => IFTHENELSE(ISNEG(m))(NEG(m))(m);
- const NAT = m => m(a => b => SUB(a)(b)); //converts an int to a natural where negatives go to zero
- const EQINT = m => n => m(a => b => n(c => d => EQ(ADD(a)(d))(ADD(b)(c))));
- const LEQINT = m => n => m(a => b => n(c => d => LEQ(ADD(a)(d))(ADD(b)(c))));
- const GEQINT = m => n => LEQINT(n)(m);
- const LTINT = m => n => NOT(GEQINT(m)(n));
- const GTINT = m => n => NOT(LEQINT(m)(n));
- const POWINT = b => e => e(x => MULTINT(b)(x))(ONEINT); //handles whole number powers of integers - no fractional or negative powers
- const ZEROINT = INT(ZERO);
- const ONEINT = INT(ONE);
- const TWOINT = INT(TWO);
- const THREEINT = INT(THREE);
- const FOURINT = INT(FOUR);
- const FIVEINT = INT(FIVE);
- const MINUSONE = NEG(ONEINT);
- const MINUSTWO = NEG(TWOINT);
- const MINUSTHREE = NEG(THREEINT);
- const MINUSFOUR = NEG(FOURINT);
- const MINUSFIVE = NEG(FIVEINT);
- const RAT = n => PAIR(n)(ONEINT); //converts church integer to church rational
- const RECIP = r => r(p => q => PAIR(q)(p));
- const NEGRAT = r => r(p => q => PAIR(NEG(p))(q));
- const ADDRAT = p => q => p(a => b => q(c => d => PAIR(ADDINT(MULTINT(a)(d))(MULTINT(b)(c)))(MULTINT(b)(d))));
- const SUBRAT = p => q => ADDRAT(p)(NEGRAT(q));
- const MULTRAT = p => q => p(a => b => q(c => d => PAIR(MULTINT(a)(c))(MULTINT(b)(d))));
- const DIVRAT = p => q => MULTRAT(p)(RECIP(q));
- const POWRAT = b => e => IFTHENELSE(ISNEG(e))(NAT(ABS(e))(r => DIVRAT(r)(b))(ONERAT))(NAT(e)(r => MULTRAT(r)(b))(ONERAT)); //allows negative powers in b^e, which divide 1 by b e times
- const SIMPRAT = r => r(p => q => PAIR(DIVINT(p)(GCDINT(p)(q)))(DIVINT(q)(GCDINT(p)(q)))); //simplify a rational - aggressively slow
- const ABSRAT = r => r(p => q => PAIR(ABS(p))(ABS(q)));
- const ISZERORAT = r => r(p => q => AND(ISZEROINT(p))(NOT(ISZEROINT(q))));
- const ISNEGRAT = r => r(p => q => AND(XOR(ISNEG(p))(ISNEG(q)))(NOT(ISZERORAT(r))));
- const ISPOSRAT = r => NOT(OR(ISNEGRAT(r))(ISZERORAT(r)));
- const EQRAT = p => q => p(a => b => q(c => d => EQINT(MULTINT(a)(d))(MULTINT(b)(c))));
- const LEQRAT = p => q => p(a => b => q(c => d => OR(AND(ISPOS(MULTINT(b)(d)))(LEQINT(MULTINT(a)(d))(MULTINT(b)(c))))(AND(ISNEG(MULTINT(b)(d)))(LEQINT(MULTINT(b)(c))(MULTINT(a)(d))))));
- const GEQRAT = p => q => LEQRAT(q)(p);
- const LTRAT = p => q => NOT(GEQRAT(p)(q));
- const GTRAT = p => q => NOT(LEQRAT(p)(q));
- const ZERORAT = RAT(ZEROINT);
- const ONERAT = RAT(ONEINT);
- const TWORAT = RAT(TWOINT);
- const THREERAT = RAT(THREEINT);
- const ONEHALF = DIVRAT(ONERAT)(TWORAT);
- const ONETHIRD = DIVRAT(ONERAT)(THREERAT);
- const ONEQUARTER = DIVRAT(ONEHALF)(TWORAT);
- const TWOTHIRDS = MULTRAT(ONETHIRD)(TWORAT);
- const THREEQUARTERS = MULTRAT(ONEQUARTER)(THREERAT);
- //for positive rationals can work directly with church naturals for the numerator and denominator rather than church integers - can be faster for complex calculations
- const POSRAT = n => PAIR(n)(ONE); //converts church integer to positive church rational
- const ADDPOSRAT = p => q => p(a => b => q(c => d => PAIR(ADD(MULT(a)(d))(MULT(b)(c)))(MULT(b)(d))));
- const SUBPOSRAT = p => q => p(a => b => q(c => d => PAIR(SUB(MULT(a)(d))(MULT(b)(c)))(MULT(b)(d))));
- const MULTPOSRAT = p => q => p(a => b => q(c => d => PAIR(MULT(a)(c))(MULT(b)(d))));
- const DIVPOSRAT = p => q => MULTPOSRAT(p)(RECIP(q));
- const POWPOSRAT = b => e => IFTHENELSE(ISNEG(e))(NAT(ABS(e))(r => DIVPOSRAT(r)(b))(ONEPOSRAT))(NAT(e)(r => MULTPOSRAT(r)(b))(ONEPOSRAT)); //allows negative powers in b^e, which divide 1 by b e times
- const SIMPPOSRAT = r => r(p => q => PAIR(DIV(p)(GCD(p)(q)))(DIV(q)(GCD(p)(q)))); //simplify a rational - aggressively slow
- const ISZEROPOSRAT = r => r(p => q => AND(ISZERO(p))(NOT(ISZERO(q))));
- const LEQPOSRAT = p => q => p(a => b => q(c => d => LEQ(MULT(a)(d))(MULT(b)(c))));
- const GEQPOSRAT = p => q => LEQPOSRAT(q)(p);
- const EQPOSRAT = p => q => p(a => b => q(c => d => EQ(MULT(a)(d))(MULT(b)(c))));
- const LTPOSRAT = p => q => NOT(GEQPOSRAT(p)(q));
- const GTPOSRAT = p => q => NOT(LEQPOSRAT(p)(q));
- const SQRT = r => i => THREE(x => DIVPOSRAT(ADDPOSRAT(x)(DIVPOSRAT(r)(x)))(TWOPOSRAT))(i); //uses three iterations of newton's method to find sqrt(r) with an initial guess of i - slow but calculates to ~6 decimal places for a good guess. four iterations is technically doable but extremely slow even for sqrt(2) (and gets to ~11 d.p.)
- const CUBERT = r => i => TWO(x => DIVPOSRAT(ADDPOSRAT(MULTPOSRAT(TWOPOSRAT)(x))(DIVPOSRAT(r)(MULTPOSRAT(x)(x))))(THREEPOSRAT))(i) //uses two iterations of newton's method to find cube root(r) with initial guess of i - very slow and not quite as accurate (~3 d.p.) but three iterations is not doable as the fractions become impractically large (about 10^18 in the numerator and denominator for cube root of 2)
- const ZEROPOSRAT = POSRAT(ZERO);
- const ONEPOSRAT = POSRAT(ONE);
- const TWOPOSRAT = POSRAT(TWO);
- const THREEPOSRAT = POSRAT(THREE);
- const POSONEHALF = DIVPOSRAT(ONEPOSRAT)(TWOPOSRAT);
- const POSONETHIRD = DIVPOSRAT(ONEPOSRAT)(THREEPOSRAT);
- const POSONEQUARTER = MULTPOSRAT(POSONEHALF)(POSONEHALF);
- const POSTWOTHIRDS = MULTPOSRAT(POSONETHIRD)(TWOPOSRAT);
- const POSTHREEQUARTERS = MULTPOSRAT(POSONEQUARTER)(THREEPOSRAT);
- const WHILE_GEN = c => f => r => x => IFTHENELSE(c(x))(() => r(f(x)))(() => x)();
- const WHILE = c => f => Y(WHILE_GEN(c)(f)); //while and for can be pretty slow and are best suited to operations on natural numbers
- //takes condition, body function, and initial value and applies the function to the value so long as the condition is met
- const FOR_GEN = c => g => f => r => x => i => IFTHENELSE(c(i))(() => r(f(x)(i))(g(i)))(() => x)();
- const FOR = c => g => f => Y(FOR_GEN(c)(g)(f));
- //takes a condition, iterator function, body function, initial value and initial index and applies the body function to the value and index and the iterator function to the index so long as the condition is met on the index
- //Self-encoding lambda calculus - if you self-encode a lambda expression with VAR, APP, and LAM (like LAM(f => LAM(x => APP(VAR(f))(VAR(x)))) for \f.\x.f x) then E will convert the encoded version to a beta reduction of the equivalent lambda expression, and R will produce a self-encoded beta reduction of the equivalent lambda expression
- const E = Y(e => m => m(x => x)(m => n => (e(m))(e(n)))(m => v => e(m(v))));
- const P = Y(p => m => (x => x(v => p(a => b => c => b(m)(v(a => b => b))))(m)));
- const RR = Y(r => m => m(x => x)(m => n => (r(m))(a => b => a)(r(n)))(m => (g => x => x(g)(a => b => c => c(w => g(P(a => b => c => a(w)))(a => b => b))))(v => r(m(v)))));
- const R = m => RR(m)(a => b => b);
- const VAR = m => a => b => c => a(m)
- const APP = m => n => a => b => c => b(m)(n);
- const LAM = f => a => b => c => c(f)
- const ToBool = x => x(true)(false); //converts church bool into primitive bool
- const ToNatural = x => x(x => x + 1)(0); //converts church natural to primitive natural
- const ToArray = x => x(x => y => [x, y]); //converts a church pair to an array
- const ToList = l => l(x => y => [x, ...y])([]); //converts a church list of arbitrary length to an array
- const ToInt = x => x(x => y => ToNatural(x) - ToNatural(y)); //converts a church integer to a primitive integer
- const ToPosFloat = x => x(x => y => ToNatural(x) / ToNatural(y)); //converts church positive rational to primitive float
- const ToPosFraction = x => x(p => q => ToNatural(p) + "/" + ToNatural(q)); //converts a church positive rational to a string representing its fraction form
- const ToSimplifiedPosFraction = x => ToPosFraction(SIMPPOSRAT(x)); //converts a church rational to a string representing its simplified fractional form - EXTREMELY SLOW
- const ToFloat = x => x(x => y => ToInt(x) / ToInt(y)); //converts church rational to primitive float
- const ToFraction = x => x(p => q => IFTHENELSE(ISNEG(q))(ToInt(NEG(p)) + "/" + ToInt(NEG(q)))(ToInt(p) + "/" + ToInt(q))); //converts a church rational to a string representing its fraction form
- const ToSimplifiedFraction = x => ToFraction(SIMPRAT(x)); //converts a church positive rational to a string representing its simplified fractional form - UNFATHOMABLY SLOW
- console.log("Boolean Logic");
- console.log(ToBool(AND(TRUE)(FALSE)));
- console.log(ToBool(AND(TRUE)(TRUE)));
- console.log(ToBool(OR(FALSE)(FALSE)));
- console.log(ToBool(OR(FALSE)(TRUE)));
- console.log(ToBool(XOR(TRUE)(FALSE)));
- console.log(ToBool(XOR(TRUE)(TRUE)));
- console.log(ToBool(EQBOOL(TRUE)(TRUE)));
- console.log(ToBool(EQBOOL(FALSE)(FALSE)));
- console.log(ToBool(EQBOOL(TRUE)(FALSE)));
- console.log("Conditionals");
- console.log(ToNatural(IFTHENELSE(TRUE)(ONE)(TWO)));
- console.log(ToNatural(IFTHENELSE(FALSE)(ONE)(TWO)));
- console.log("Arithmetic");
- console.log(ToNatural(ZERO));
- console.log(ToNatural(SUCC(ZERO)));
- console.log(ToNatural(SUCC(ONE)));
- console.log(ToNatural(ADD(THREE)(SEVEN)));
- console.log(ToNatural(MULT(EIGHT)(THREE)));
- console.log(ToNatural(POW(THREE)(FOUR)));
- console.log("Subtraction");
- console.log(ToNatural(PRED(ONE)));
- console.log(ToNatural(PRED(FOUR)));
- console.log(ToNatural(SUB(EIGHT)(THREE)));
- console.log("Zero detection, equality, and ordering");
- console.log(ToBool(ISZERO(ZERO)));
- console.log(ToBool(ISZERO(ONE)));
- console.log(ToBool(LEQ(TWO)(FIVE)));
- console.log(ToBool(LEQ(FIVE)(TWO)));
- console.log(ToBool(LEQ(FIVE)(FIVE)));
- console.log(ToBool(LT(FIVE)(TWO)));
- console.log(ToBool(LT(TWO)(FIVE)));
- console.log(ToBool(LT(FIVE)(FIVE)));
- console.log(ToBool(GT(FIVE)(TWO)));
- console.log(ToBool(GT(TWO)(FIVE)));
- console.log(ToBool(GT(FIVE)(FIVE)));
- console.log(ToBool(GEQ(FIVE)(FIVE)));
- console.log(ToBool(GEQ(FIVE)(TWO)));
- console.log(ToBool(GEQ(TWO)(FIVE)));
- console.log(ToBool(EQ(TWO)(FIVE)));
- console.log(ToBool(EQ(TWO)(TWO)));
- console.log("Lists and list functions");
- console.log(ToList(MAP(EXAMPLELIST)(ToNatural)));
- console.log(ToList(MAP(MAP(EXAMPLELIST)(x => SUCC(x)))(ToNatural)));
- console.log(ToNatural(REDUCE(EXAMPLELIST)(x => y => ADD(x)(y))(ZERO)));
- console.log(ToNatural(LENGTH(EXAMPLELIST)));
- console.log(ToList(MAP(FILTER(EXAMPLELIST)(x => GT(x)(TWO)))(ToNatural)));
- console.log(ToList(MAP(APPEND(EXAMPLELIST)(SIX))(ToNatural)));
- console.log(ToList(MAP(REVERSE(EXAMPLELIST))(ToNatural)));
- console.log(ToList(MAP(INF(TEN))(ToNatural)));
- console.log(ToList(MAP(RANGE(THREE)(TEN))(ToNatural)));
- console.log(ToNatural(INDEX(EXAMPLELIST)(THREE)));
- console.log(ToList(MAP(TAKE(EXAMPLELIST)(TWO))(ToNatural)));
- console.log("List head and tail");
- console.log(ToNatural(HEAD(EXAMPLELIST)));
- console.log(ToList(MAP(TAIL(EXAMPLELIST))(ToNatural)));
- console.log(ToNatural(FOOT(EXAMPLELIST)));
- console.log(ToList(MAP(BODY(EXAMPLELIST))(ToNatural)));
- console.log("Pairs");
- console.log(ToNatural(FIRST(PAIR(ONE)(TWO))));
- console.log(ToNatural(SECOND(PAIR(ONE)(TWO))));
- console.log(ToArray(PAIR(ToNatural(THREE))(ToNatural(ZERO))));
- console.log("Recursion");
- console.log(ToNatural(FACT(EIGHT)));
- console.log(ToNatural(FIB(EIGHT)));
- console.log(ToList(MAP(MAP(INF(SEVEN))(FACT))(ToNatural)));
- console.log(ToList(MAP(MAP(INF(TEN))(FIB))(ToNatural)));
- console.log(ToBool(ISEVEN(EIGHT)));
- console.log(ToBool(ISEVEN(FIVE)));
- console.log(ToList(MAP(FILTER(EXAMPLELIST)(ISEVEN))(ToNatural)));
- console.log(ToList(MAP(FILTER(EXAMPLELIST)(ISODD))(ToNatural)));
- console.log(ToList(MAP(MAP(EXAMPLELIST)(x => IFTHENELSE(ISEVEN(x))(MULT(x)(TWO))(FIB(x))))(ToNatural)));
- console.log(ToNatural(MOD(SEVEN)(THREE)));
- console.log(ToNatural(MOD(NINE)(THREE)));
- console.log(ToNatural(DIV(NINE)(TWO)));
- console.log(ToNatural(GCD(EIGHT)(SIX)));
- console.log(ToNatural(GCD(NINE)(FIVE)));
- console.log("Integers");
- console.log(ToInt(FOURINT));
- console.log(ToInt(MINUSFOUR));
- console.log(ToInt(ADDINT(TWOINT)(FIVEINT)));
- console.log(ToInt(SUBINT(TWOINT)(FIVEINT)));
- console.log(ToInt(MULTINT(TWOINT)(FIVEINT)));
- console.log(ToInt(MULTINT(TWOINT)(MINUSFIVE)));
- console.log(ToBool(EQINT(ADDINT(TWOINT)(THREEINT))(FIVEINT)));
- console.log(ToBool(EQINT(ADDINT(TWOINT)(THREEINT))(FOURINT)));
- console.log(ToBool(EQINT(SUBINT(TWOINT)(THREEINT))(MINUSONE)));
- console.log(ToBool(EQINT(SUBINT(TWOINT)(THREEINT))(MINUSTWO)));
- console.log(ToInt(DIVINT(FOURINT)(TWOINT)));
- console.log(ToInt(DIVINT(FIVEINT)(TWOINT)));
- console.log(ToInt(DIVINT(FOURINT)(MINUSTWO)));
- console.log(ToInt(DIVINT(FIVEINT)(MINUSTWO)));
- console.log(ToInt(DIVINT(MINUSFOUR)(TWOINT)));
- console.log(ToInt(DIVINT(MINUSFIVE)(TWOINT)));
- console.log(ToInt(DIVINT(MINUSFOUR)(MINUSTWO)));
- console.log(ToInt(DIVINT(MINUSFIVE)(MINUSTWO)));
- console.log(ToInt(POWINT(TWOINT)(THREE)));
- console.log(ToInt(POWINT(MINUSTWO)(THREE)));
- console.log(ToInt(POWINT(MINUSTWO)(FOUR)));
- console.log(ToInt(MODINT(FIVEINT)(THREEINT)));
- console.log(ToInt(MODINT(MINUSFIVE)(THREEINT)));
- console.log(ToInt(MODINT(FIVEINT)(MINUSTHREE)));
- console.log(ToInt(MODINT(MINUSFIVE)(MINUSTHREE)));
- console.log(ToInt(GCDINT(FOURINT)(TWOINT)));
- console.log(ToInt(GCDINT(INT(TEN))(INT(SIX))));
- console.log(ToInt(SUCCINT(TWOINT)));
- console.log(ToInt(SUCCINT(ZEROINT)));
- console.log(ToInt(SUCCINT(MINUSTWO)));
- console.log(ToInt(SUCCINT(MINUSONE)));
- console.log("Rationals");
- console.log(ToFloat(ONERAT));
- console.log(ToFloat(ONEHALF));
- console.log(ToFloat(TWOTHIRDS));
- console.log(ToFloat(NEGRAT(ONEHALF)));
- console.log(ToFloat(RECIP(ONEHALF)));
- console.log(ToFloat(RECIP(THREEQUARTERS)));
- console.log(ToFloat(ADDRAT(ONEHALF)(ONETHIRD)));
- console.log(ToFloat(SUBRAT(ONEHALF)(ONETHIRD)));
- console.log(ToFloat(MULTRAT(THREEQUARTERS)(TWOTHIRDS)));
- console.log(ToFloat(DIVRAT(ONEHALF)(ONETHIRD)));
- console.log(ToFloat(DIVRAT(ONERAT)(ZERORAT)));
- console.log(ToFloat(ADDRAT(DIVRAT(ONERAT)(ZERORAT))(ONERAT)));
- console.log(ToFloat(NEGRAT(DIVRAT(ONERAT)(ZERORAT))));
- console.log(ToFloat(RECIP(ADDRAT(DIVRAT(ONERAT)(ZERORAT))(ONERAT))));
- console.log(ToFloat(POWRAT(TWOTHIRDS)(FOURINT)));
- console.log(ToFloat(POWRAT(TWOTHIRDS)(MINUSFOUR)));
- console.log(ToPosFloat(SQRT(TWOPOSRAT)(RECIP(POSTWOTHIRDS))));
- console.log(ToPosFloat(SQRT(POSRAT(FIVE))(TWOPOSRAT)));
- console.log(ToPosFloat(SQRT(POSRAT(TEN))(THREEPOSRAT)));
- console.log(ToPosFloat(CUBERT(TWOPOSRAT)(RECIP(POSTWOTHIRDS))));
- console.log(ToFraction(ONEHALF));
- console.log(ToFraction(POWRAT(TWOTHIRDS)(FOURINT)));
- console.log(ToPosFraction(SQRT(TWOPOSRAT)(RECIP(POSTWOTHIRDS))));
- console.log(ToFraction(DIVRAT(RAT(INT(SIX)))(NEGRAT(RAT(INT(TEN))))));
- console.log(ToSimplifiedFraction(DIVRAT(NEGRAT(RAT(INT(SIX))))(RAT(INT(TEN)))));
- console.log("While and for loops");
- console.log(ToList(MAP(REVERSE(WHILE(x => NOT(EQ(HEAD(x))(ONE)))(x => IFTHENELSE(ISEVEN(HEAD(x)))(CONS(DIV(HEAD(x))(TWO))(x))(CONS(ADD(MULT(THREE)(HEAD(x)))(ONE))(x)))(CONS(SEVEN)(NIL))))(ToNatural))); //Collatz sequence for 7
- console.log(ToNatural(FOR(i => LEQ(i)(ONEHUNDRED))(SUCC)(x => i => ADD(x)(i))(ZERO)(ONE))); //Sum of natural numbers from 0 to 100
- console.log("Self-encoding lambda calculus");
- console.log(ToNatural(E(APP(LAM(n => LAM(f => LAM(x => APP(VAR(f))(APP(APP(VAR(n))(VAR(f)))(VAR(x)))))))(LAM(f => LAM(x => APP(VAR(f))(VAR(x))))))));
- console.log(ToNatural(E(R(LAM(f => LAM(x => APP(VAR(f))(VAR(x))))))));
- console.log(ToBool(E(R(LAM(x => LAM(y => VAR(x)))))));
- console.log(ToPosFloat(E(APP(APP(VAR(SQRT))(VAR(TWOPOSRAT)))(APP(VAR(RECIP))(VAR(POSTWOTHIRDS))))));
- console.log("Primes up to 200 by Sieve of Eratosthenes")
- console.log(ToList(MAP(FILTER(FILTER(FILTER(FILTER(RANGE(TWO)(MULT(ONEHUNDRED)(TWO)))(x => NOT(AND(ISEVEN(x))(GT(x)(TWO)))))(x => NOT(AND(ISZERO(MOD(x)(THREE)))(GT(x)(THREE)))))(x => NOT(AND(ISZERO(MOD(x)(FIVE)))(GT(x)(FIVE)))))(x => NOT(AND(ISZERO(MOD(x)(SEVEN)))(GT(x)(SEVEN)))))(ToNatural)));
Advertisement