Created
March 7, 2022 10:36
-
-
Save intarga/25b110b1b933826fe6f22c54429dcb93 to your computer and use it in GitHub Desktop.
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
const cons = (head, tail) => | |
bool => bool ? head : tail | |
const head = pair => pair(true) | |
const tail = pair => pair(false) | |
const pairToString = pair => "(" + head(pair) + "," + tail(pair) + ")" | |
const myPair = cons(1, 2) | |
console.log(pairToString(myPair)) | |
console.log(head(myPair)) | |
console.log(tail(myPair)) | |
const list = arr => { | |
arr.push(null); | |
return arr.reduceRight((acc, curr) => cons(curr, acc)) | |
} | |
const listToString = list => { | |
const elems = list => | |
list == null ? | |
"" : | |
" " + head(list) + elems(tail(list)) | |
return "(" + elems(list).substring(1) + ")" | |
} | |
const myList = list([1, 2, 3, 4, 5]) | |
console.log(listToString(myList)) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
A true 4x4 masterpiece!