Last active
April 14, 2022 16:43
-
-
Save noahhaasis/25c74df150b13debbe10ea988b52a540 to your computer and use it in GitHub Desktop.
This file contains hidden or 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
const z = f => x => x | |
const succ = n => f => x => f(n(f)(x)) | |
function encode_numeral(n) { | |
if (n === 0) return z; | |
return succ(encode_numeral(n-1)); | |
} | |
function pp_numeral(n) { | |
const pp_rest = r => r === 'x' ? 'f x' : `f (${r})`; | |
return "λf.λx. " + n(r => pp_rest(r))("x"); | |
} | |
const nil = c => n => n | |
const cons = e => l => c => n => c(e)(l(c)(n)) | |
function encode_list(l) { | |
if (l.length === 0) return nil; | |
const [x, ...xs] = l; | |
return cons(x)(encode_list(xs)) | |
} | |
function encode_string(s) { | |
const char_list = Array.from(s).map(c => c.charCodeAt(0)); | |
return encode_list(char_list.map(encode_numeral)) | |
} | |
function pp_string(l) { | |
const pp_concat = e => l => l === 'n' ? `c (${pp_numeral(e)}) n` : `c (${pp_numeral(e)}) (${l})`; | |
return `λc.λn. ${l(pp_concat)('n')}`; | |
} | |
console.log(pp_string(encode_string('bugs'))); |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment