📜 ⬆️ ⬇️

Factorial on Church numbers - now in emoticons

Good morning everyone




This is a fully valid JavaScript code.

Of course, not in this form.

Yesterday, flipping through the list of things left for later, I came across an article on the calculation of factorial on the Church numbers , I found in the comments a proposal to implement on arrow functions.
')
But since April 1 is already here, you need something interesting and unusual.

var fact = ((cat) => (cat) (cat)) ((cat) => ((drunk) => ((idea) => (hrv) => ((bug) => (facepalm) => (swear) => (bug) (facepalm) (swear)) (((hrv) => (hrv) ((cat) => (cat) => (drunk) => (drunk)) ((cat) => (drunk) => (cat))) (hrv)) (((hrv) => (idea) => (cat) => (idea) ((hrv) (idea) (cat))) ((idea) => (cat) => (cat))) ((cat) => ((hrv) => (dog) => (idea) => (hrv) ((dog) (idea))) (hrv) ((idea) (((hrv) => (wasntme) => (tmi) => ((bug) => (bug) ((cat) => (drunk) => (drunk))) ((hrv) ((bug) => ((devil) => (heidy) => (facepalm) => (facepalm) (devil) (heidy) ) ((wasntme) (((bug) => (bug) ((cat) => (drunk) => (cat))) (bug))) (((bug) => (bug) ((cat) => (drunk) => (cat))) (bug))) (((devil) => (heidy) => (facepalm) => (facepalm) (devil) (heidy)) (tmi) (tmi)))) (hrv))) (cat))) ((cat) (cat)) (drunk))); 

(Send via Skype).


Here are the other functions, already in a simpler and more natural way:
 var True = (x) => (y) => x; var False = (x) => (y) => y; var If = (p) => (t) => (e) => p(t)(e); // example: If(True)('foo')('bar') var Zero = (f) => (x) => x; var Succ = (n) => (f) => (x) => f(n(f)(x)); var IsZero = (n) => n((x) => False)(True); var Mul = (n) => (m) => (f) => n(m(f)); var Pair = (a) => (b) => (t) => t(a)(b); var Fst = (p) => p(True); var Snd = (p) => p(False); var Pred = (n) => (s) => (z) => Snd(n((p) => Pair(s(Fst(p)))(Fst(p)) )(Pair(z)(z))); 


For how it all works - in the original article .
Well, use this:
 fact(Succ(Succ(Succ(Zero))))(function (x) { return x + 1; })(0); // => 6 

(where Succ is the addition of one).

All the holiday!


UPD:

Apparently, on this holiday thoughts converge:
Programming emoticons.

Source: https://habr.com/ru/post/254587/


All Articles