Skip to content

Instantly share code, notes, and snippets.

@intarga
Created March 7, 2022 10:36
Show Gist options
  • Save intarga/25b110b1b933826fe6f22c54429dcb93 to your computer and use it in GitHub Desktop.
Save intarga/25b110b1b933826fe6f22c54429dcb93 to your computer and use it in GitHub Desktop.
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))
@anafvana
Copy link

A true 4x4 masterpiece!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment