Skip to content

Instantly share code, notes, and snippets.

@Pet3ris
Forked from 140bytes/LICENSE.txt
Created May 24, 2011 11:06
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save Pet3ris/988538 to your computer and use it in GitHub Desktop.
Save Pet3ris/988538 to your computer and use it in GitHub Desktop.
Y combinator in 140byt.es
function(
// we take Haskell's fixed point combinator
// λf.(λx.f(xx))(λx.f(xx))
// and add an additional argument (_ stands for arguments of function)
// λf.(λx_.f(xx)_)(λx_.f(xx)_)
// abstract further
// λf.(λg.gg)(λx_.f(xx)_)
// curry the last bit
// λf.(λg.gg)(λx.λ_.f(xx)_)
// and alpha-rename variables (note there is no collision with the c's)
// λa.(λc.cc)(λc.λ_.a(cc)_)
// or more explictly
// λa.(λc.cc)(λc.λ"arguments".a(cc)"arguments")
a, // λa.
// a function that calls its argument as if it was itself, e.g., the identity function(I){return function(n){return n==0?0:I(n-1)+1}}
b // undefined placeholder for pure functions or this. for the recursive function a
){
return( // (
function(c){ // λc.
return c(c) // cc
}
) // )
( // (
function(c){ // λc.
return function(){ // λ_.
return a(c(c)). // a(cc)
apply(b,arguments) // _
}
}
) // )
}
function(a,b){return(function(c){return c(c)})(function(c){return function(){return a(c(c)).apply(b,arguments)}})}
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
Version 2, December 2004
Copyright (C) 2011 Peteris Erins http://peteriserins.com
Everyone is permitted to copy and distribute verbatim or modified
copies of this license document, and changing it is allowed as long
as the name is changed.
DO WHAT THE FUCK YOU WANT TO PUBLIC LICENSE
TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION
0. You just DO WHAT THE FUCK YOU WANT TO.
{
"name": "ycombinator",
"description": "A Y combinator.",
"keywords": [
"combinator",
"functional",
"lambda"
]
}
@Kambfhase
Copy link

Would you please provide an example? I dont understand how to call this mess of functions.

@Pet3ris
Copy link
Author

Pet3ris commented Nov 12, 2011

If we assign this function to Y, say, then we can define the recursive identity function on integers as follows:

id = Y(function(id) { return function(n) { return n == 0 ? 0 : id(n - 1) + 1 }}

@Kambfhase
Copy link

Ah, ok, now I get it. You might be able to save some bytes if you were using more imperative features. I found a version which is shorter than yours and has to be used differently:

(
  function(a){return function b(){return a.apply({},[b].concat([].slice.call(arguments)))}}
)(
  function (self, n){ return n==0? 1 : n* self( n-1);}
)( 5) // 120

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