9 ms·
Understanding the Y Combinator
- arabello 2y agoMy big brain expected startup stuff
- denysvitali 2y agoI mean, posting about the other Y Combinator on... Y Combinator
- deleted 2y ago[deleted]
- redbell 2y agoWell, I have to confess I was misled by the title! I was under the impression that the article was about YC, but it turned out to be about mathematics. Ironically, the below quote is still relevant to YC, I believe: > 8. Applications of the Y combinator > You might be wondering what makes the Y combinator so special.
- a-s-k-af 2y agoYou are not alone mate :p
- nextaccountic 2y agoThe name Y Combinator is a metaphor: startup growth is "recursive" in a sense and Y Combinator builds such "recursive" companies out of a non-recursive input (the founders themselves) Also Paul Graham is a Lisp nerd (Note, I tried to find an authoritative source on how the name was chosen - the only thing I found was https://paulgraham.com/ycstart.html https://paulgraham.com/ycstart.html that lists when, but not how the name came to be; but I think this metaphor is somewhat obvious)
- kazinator 2y agoTo me, the "combinator" part is quite obviously an earcorn pun on "incubator".
- dotancohen 2y ago> Well, I have to confess I was misled by the title! I was under the impression that the article was about YC, but it turned out to be about mathematics. What gave it away for me was the define article "the". The "the" would have been absent had the article been about the company. That was very difficult to write while avoiding puns!
- deleted 2y ago[deleted]
- mikewarot 2y agoI'd really like to understand why my brain screams NO when I try to grok lisp and especially the Y combinator. It's like it's anti-interesting to me or something. I know it's cool, and useful to know, but I just can't get there. Maybe it's a stack overflow in my head? Is it just me? [Edit] Re:082349872349872 - I grew up in a world of assembler programming, BASIC and Pascal. It wasn't unusual to have all sorts of self-interacting code, and deliberate use of the same half of a routine in more than one function. I get why Lambda is useful, why bother naming a function you're only going to use once. But it seemed like infinite pedantry to go about using it to do recursion... thanks! [Edit2] Re:082349872349872 - I once came upon old code that lived in a S-100 disk controller, it seemed really weird until I realized they couldn't call a subroutine because they couldn't assume there was a properly set stack. It used one of the 8080 registers as a return pointer.
- sph 2y agoIt might be you. But your profile says you have been programming since 1979, so I humbly defer to your experience. If you have made it this far without Lisp, I guess you don't need it, friend :-) Take it from a younger programmer, you don't have to grok lambda calculus to understand Lisp. I like Lisp, but I only have a vague notion of lambda calculus and all that academic gobbledygook. I still do not understand the Y combinator, nor I very much care to. CS theory is good and all, but it's importance is often overstated. Physicists are not engineers and engineers are not physicists; you can be a very effective programmer without knowing about Peano numbers and what monads are.
- 082349872349872 2y agoIf you grew up programming in a world where self-reference is (due to linking pass or interpretation) trivial, the Y combinator is nearly useless: why should a function not be able to call itself, like we do every day? However, if you're a pedant, and only accept definitions that are in terms of things other than themselves, then you'll need something like the Y combinator to even define, let alone invoke, recursive functions. (there was a period* of a decade or two where systems people wrote recursive functions and theory people said "yes, it works in practice, but does it work in theory?", but now even theorists have a pedantic way to both describe functions in terms of themselves [syntactically] and agree that such a description denotes a unique function [semantically], so now we have our cake and eat it: a toolchain will go right ahead and generate fixups for the object that point back into itself, while a thesis will drop names like Tarski or Brouwer and maybe typeset another greek character or two) * which may have overlapped with the period where people preferred one-pass to two-pass tools because manually stuffing paper tape back into the reader between passes was a pain? EDIT: extreme pedantry: it surprises me that there was also a decade or so between LABEL and LABELS; going from single recursion to mutual recursion is a very small change in the underlying interpreters, yet even lispers were too pragmatic to have bothered to do so. EDIT2: upon reflection, this was also the period of time where having a stack was viewed as a complexity which was certainly convenient but not exactly necessary. With the benefit of hindsight, I'd argue that mutual tail recursion is useful even without a stack, but can see that at the time that might've been viewed as architecture astronaut talk.
- tromp 2y agoNote that with the standard representation of natural numbers in lambda calculus, the Church numerals [1], you don't even need the Y combinator to implement factorial: fac = λn.λf.n(λf.λn.n(f(λf.λx.n f(f x))))(λx.f)(λx.x) For example, applied to Church numeral 3 this gives (with F=(λf.λn.n(f(λf.λx.n f(f x))))): fac 3 = \f. F (F (F (\x.f))) 1 = \f. 1 (F (F (\x.f)) 2) = \f. 1 (2 (F (\x.f) 3)) = \f. 1 (2 (3 ((\x.f) 4))) = \f. 1 (2 (3 f)) which is the Church numeral for 1*2*3 = 6. [1] https://en.wikipedia.org/wiki/Church_encoding https://en.wikipedia.org/wiki/Church_encoding
- quibono 2y agoHey tromp, do you have any recommendations for grokking Lambda calculus properly? I've read a few tutorials and introductions, and I understand the notions of alpha- and beta-reductions, abstractions vs application, but I still have issues with practical examples like the above. Am I missing something here or should I just go back to the basics?
- tromp 2y ago> I still have issues with practical examples like the above. What issues would those be? Do you see that λf.λx.n f(f x)) is the Church numeral n+1 ? I think there's few better ways to grok lambda calculus than working through examples like these step-by-step.
- saberience 2y agoWhat's a church numeral?
- Rygian 2y agoThe key realization for me was that in lambda calculus, "A function can’t call itself by name, so we will have to find an alternative way." The rest is smart acrobatics.
- bmacho 2y agoA 'function' can't call itself (by name or by any other method), but since 'functions' can call each other, and can make each other to call each other, recursion is mostly trivial. You try it, then you'll get a working recursion one way or another. (Lambda calculus don't operate on functions, but on expressions/lambda terms.)
- Joker_vD 2y agolet odd'(n, odd, even) = if n == 0 then False else even(n-1, odd, even) let even'(n, odd, even) = if n == 0 then True else odd(n-1, odd, even) let odd(n) = odd'(n, odd', even') let even(n) = even'(n, odd', even') Look ma, no hands! Well, actually, it is explicit closure-conversion done by hand so... anyhow, there are more straightforward and performant ways to get recursion in practice.
- marvinborner 2y agoIn lambda calculus, you could use a variadic fixed point combinator to solve such recurrence relations elegantly
- Joker_vD 2y agoBut you don't need to solve these, they're solved already: these four definitions are non-recursive. Yet when evaluated, they will exhibit properly recursive behaviour. The only reason to use Y combinator in practice is when you for some reason don't want to keep manually passing the function to itself like "func fact(self, n) { return (n < 1) ? 1 : n * self(self, n-1) }; print(fact(fact, 5))" — maybe because it's tedious and error-prone, — and don't have a sufficiently ergonomic term-rewrite system at hand that would do this for you.
- ewuhic 2y agoCool article, BUT! it leaves so many terms unexplained: - What's the distinction between a "parameter`, a "variable", and an "argument"? - What is "body" as in "Body M"? - What's a "bound" variable? - What exactly is a "function call"? - In "... a value that is mapped to itself by the function.", what do "mapped to itself", and, more specifically, "mapped" mean? Would appreciate the answers!
- seanhunter 2y agoI'll take one of these. > In "... a value that is mapped to itself by the function.", what do "mapped to itself", and, more specifically, "mapped" mean? A mathematical function is something more general than a function in programming. In maths, a function consists of two sets known as the domain and codomain and a rule which "maps" things in the domain into the associated things in the codomain[1]. You can think about the domain as all the possible inputs to the function and the codomain being a set which at least contains all the possible outputs from the function. So say the rule of my function is f(x) = sin(x) and for simplicity I say the domain is real numbers greater than or equal to zero but less than 2pi, then the function "maps" any value in that domain to a value in its codomain (which in this case will be real numbers from -1 to 1). And what it's actually doing when it maps a number is it applies "sin" to that number to find the value in the codomain (which in computer science we would call the return value of the function). So when the author talks about fixed points being values which are mapped onto themselves by the function they are values in the domain where you apply the rule of the function and you get back the same value. So for f(x) = sin(x), 0 would be such a value because sin(0) = 0. Say my function is f(x) = x^3, then 0, 1 and -1 are fixed points, because f(-1) = (-1)^3 = -1, and you can see the same applies to f(0) and f(1). As a bonus, if my rule is f(x) = sin(x), then "x" is a "parameter" or "argument" to the function, because it is the name I give to a value that is passed into the rule of my function. It is also a variable because it's a name for a thing which varies but not all variables get used as parameters to functions. For example if I write y = f(x), then y is a variable which is not any kind of parameter for example. Hope that helps. [1] I suppose technically into the "image set" of the function, which is a subset of the codomain comprising the actual output values of the function only.
- thwg 2y agoI thought this post was about dissing Paul G, Garry Tan and the like. I was wrong.
- breck 2y agoExercise for the reader: define the YCombinator without using any letters or words.
- marvinborner 2y ago000100011100110100001110011010
- wazdra 2y agoI had the chance to follow Goubault-Larrecq's very nice course on Logic and Computation, that went over this in a very similar way. Unfortunately, the whole material is in french, but might still interest some : http://www.lsv.fr/~goubault/Lambda/loginfoindex.html http://www.lsv.fr/~goubault/Lambda/loginfoindex.html (The relevant part is the "Lambda calcul pur" one")
- m-hodges 2y agoThe "Quick introduction to lambda calculus" section is good. If you're looking to really learn about it for the first time, I wrote Exploring the Lambda Calculus with Python: https://matthodges.com/posts/2022-07-17-exploring-lambda-calculus-python/ https://matthodges.com/posts/2022-07-17-exploring-lambda-cal...
- pdonis 2y agoNote to article writers: (1) Black on white is much easier to read than white on black. That includes code snippets in blocks. (2) If you insist on ignoring (1), at least make your white text bright enough that it can be read without serious eye strain. Particularly if the white on black is code snippets surrounded by black on white article text.
- marcellus23 2y agoThe article is black on white for me. Are you sure you don't have an extension enabled or something?
- pdonis 2y agoAs the last sentence of my (2) shows, the article itself is black on white for me too, but the code snippets are white (very faint white that I can't read without serious eyestrain) on black.
- marcellus23 2y agoThe text color in those snippets is pure white, and the background is nearly pure black (#111111). I don't think the problem is with the website.
- pdonis 2y ago> The text color in those snippets is pure white Including the syntax highlighted parts? I don't think so. > I don't think the problem is with the website. I don't think you have fully considered the situation.
- marcellus23 2y agowell no, but the syntax highlighted parts are not white, they're different colors. So complaining about them being faint white doesn't make sense. > I don't think you have fully considered the situation. Hard to "consider the situation" when you won't describe what the situation actually is.
- lacker 2y agoIt's a shame that I like Y Combinator the organization so much, because I find the y combinator as a programming concept to be aesthetically displeasing. It only makes sense in the untyped lambda calculus, where types are all conflated and errors are forbidden. It relies on the fact that you can take any x and "apply x to itself". These properties are essentially gimmicks of the untyped lambda calculus. It's like the saying "Everything should be made as simple as possible, but not simpler." The untyped lambda calculus, they made it even simpler. Yes, you can reduce everything to a very tiny number of combinators, but at the cost of making everything ugly. It is much cleaner to use Lisp and reduce everything to a slightly larger set of primitives. Ironically (?) one of the best explanations of this is PG's own The Roots Of Lisp - https://languagelog.ldc.upenn.edu/myl/llog/jmc.pdf https://languagelog.ldc.upenn.edu/myl/llog/jmc.pdf
- hollerith 2y ago>It only makes sense in the untyped lambda calculus Is that really true? ISTR you can impose the type (A -> A) -> A on the Y combinator.
- hollerith 2y agoHere is Haskell code in which the Y combinator (named "fix" in the code) is given the type (a -> a) -> a: https://play.haskell.org/saved/ndm83XBr https://play.haskell.org/saved/ndm83XBr This next a very simple Haskell program that just prints the first 8 non-negative integers: main = putStrLn $ show $ take 8 $ iterate (+ 1) 0 The linked code is just an elaboration of that where instead of using the built-in "iterate" we define an equivalent "iterate2". Usually to define iterate2 a Haskell programmer would use recursion (i.e., call iterate2 inside the definition of iterate2) but today we avoid the need for a recursive call by using the Y combinator. This disproves the assertion that the Y combinator "only makes sense in the untyped lambda calculus" because the Haskell compiler gives a type to every entity in the linked code.
- lacker 2y agoI would not call that the Y combinator. As traditionally defined the Y combinator is Y = lambda f: (lambda x: f(x(x)))(lambda x: f(x(x))) so it involves applying x to x. So clearly as shown here you can express in Python. The language has to not enforce sane types. But I would say it only makes sense in the untyped lambda calculus because in any other language there would be a more reasonable way to achieve the same effect. As you show in your example, because you achieve the same effect of finding a fixed point, by not using the Y combinator.
- moi2388 2y agoFor anybody new to the lambda calculus, and wanting to learn a bit about it, I highly recommend this video series using JavaScript. https://www.youtube.com/playlist?list=PLpkHU923F2XFWv-XfVuvWuxq41h21nOPK https://www.youtube.com/playlist?list=PLpkHU923F2XFWv-XfVuvW...
- bschmidt1 2y agoIf Y-Combinator is: Y = λf.(λx.f(x x))(λx.f(x x)) And in JavaScript it's: const Y = f => (x => f(x(x)))(x => f(x(x))); Then Silicon Valley's YC must be: const YC = startup => (getMoney => startup(getMoney(getMoney)))(getMoney => startup(getMoney(getMoney)));