Last active
December 18, 2020 14:28
-
-
Save JoshCheek/d17aa2d98434ab2322d6f5c8e6bc18d6 to your computer and use it in GitHub Desktop.
Can Corey Haines figure out how to write the `block` function? (probablah nah)
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
// EXPLANATION / CHALLENGE: https://vimeo.com/492391391 | |
// The program in JavaScript: `for(let i=10; i; --i) console.log(i)` | |
// But THIS!! is the program in "lambda calculus JavaScript"!!!! | |
// Functoins that begin with a dollar sign cheat and use JS stuff | |
const $DIE = arg => { throw new Error("should not call!") } | |
// The rest of these cheat by assigning to constants instead of passing into variables, | |
// but none of them are self refferential, so that's just to make it easier to read | |
const id = x => x | |
const y = f => | |
(recur => x => f(recur(recur))(x)) | |
(recur => x => f(recur(recur))(x)) | |
const cons = l => r => f => f(l)(r) | |
const car = list => list(l => r => l) | |
const cdr = list => list(l => r => r) | |
const TRUE = trueVal => falseVal => trueVal | |
const FALSE = trueVal => falseVal => falseVal | |
const IF = bool => trueCase => falseCase => bool(trueCase)(falseCase)($DIE) | |
const noop = id | |
const inc = n => f => x => n(f)(f(x)) | |
const n0 = f => x => x | |
const n1 = inc(n0) | |
const n2 = inc(n1) | |
const n3 = inc(n2) | |
const n4 = inc(n3) | |
const n5 = inc(n4) | |
const n6 = inc(n5) | |
const n7 = inc(n6) | |
const n8 = inc(n7) | |
const n9 = inc(n8) | |
const n10 = inc(n9) | |
const isZero = n => n(bool => FALSE)(TRUE) | |
const dec = n => | |
cdr( | |
n(args => | |
IF(car(args)) | |
(_ => cons(FALSE)(cdr(args))) | |
(_ => cons(FALSE)(inc(cdr(args)))) | |
)(cons(TRUE)(n0)) | |
) | |
// !!!!!HERE is the function that eluded me for hours!!!!! | |
// Can you figure out its definition? | |
const block = () => { throw "FIX ME, COREY HAINES, I LOVE YOU!! I NEED YOU!!" } | |
// // Here are some test cases that I wrote: | |
// console.log(block(n0)(3)) // => 3 | |
// console.log(block(n1)(x => x*10)(3)) // => 30 | |
// console.log(block(n2)(x => x*10)(x => x+5)(3)) // => 35 | |
// console.log(block(n3)(x => x*10)(x => x+5)(x => x*2)(3)) // => 70 | |
const $toJs = n => n(i => i+1)(0) | |
const $print = n => console.log($toJs(n)) | |
const $wait = f => seconds => setTimeout(f, $toJs(seconds)*1000) | |
const countdown = y(recur => | |
n => | |
IF(isZero(n)) | |
(noop) | |
(block(n2) | |
(_ => $print(n)) | |
(_ => $wait(_ => recur(dec(n)))(n1)) | |
) | |
) | |
countdown(n10) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment