Y-Combinator | Playing around with different Y Combinator | Functional Programming library
kandi X-RAY | Y-Combinator Summary
kandi X-RAY | Y-Combinator Summary
Let's start with writing a simple recursive factorial function:.
Support
Quality
Security
License
Reuse
Top functions reviewed by kandi - BETA
Currently covering the most popular Java, JavaScript and Python libraries. See a Sample of Y-Combinator
Y-Combinator Key Features
Y-Combinator Examples and Code Snippets
Community Discussions
Trending Discussions on Y-Combinator
QUESTION
In this explanation of Y-combinator (https://mvanier.livejournal.com/2897.html),
...ANSWER
Answered 2021-Jun-07 at 05:53Consider the expression (define p M)
, where M
is some expression and p
is a variable.
Let's suppose we're evaluating this expression in environment E
. Evaluating (define p M)
should do two things:
- It should evaluate
M
in environmentE
; let the result bex
. - It should modify environment
E
so thatp
is bound tox
.
So what should happen when we evaluate (define factorialA (almost-factorial factorialA))
in an environment E
where factorialA
is not already defined?
First, we try to evaluate (almost-factorial factorialA)
in environment E
. To do this, we first evaluate almost-factorial
, which succeeds. We then evaluate factorialA
, which fails because factorialA
is not defined in environment E
. Thus, the whole thing should be expected to fail.
On the other hand, if we try
QUESTION
I'm trying to implement the Y-combinator like in the definition by Curry.
This code does not work. It causes infinite recursion.
...ANSWER
Answered 2021-May-05 at 16:42The first string prints out the following error:
QUESTION
I've just read "A Tutorial Introduction to the Lambda Calculus1" by Raul Rojas. I put the lambda expressions into Scheme (TinyScheme) to try them out. Everything worked except the recursive function to calculate the sum of the Church numbers 0,1,...,N using the Y-combinator which runs out of memory. Bizarrely, the Y-combinator works if I calculate the sum using regular numbers.
Here is my Y-combinator,
...ANSWER
Answered 2021-Apr-23 at 09:54The lambda calculus only works with normal order reduction (i.e. arguments are only evaluated when their values are needed), and Scheme uses applicative order reduction (arguments are evaluated first), except in "special forms".
Your code works with regular numbers not because of the numbers, but because of cond
, which will evaluate at most one of its clauses.
If you replace the cond
in your sum1
with a regular function, your computation will not terminate with regular numbers either.
QUESTION
I am attempting to build a clean and neat implementation of recursive-capable lambda self-scoping (which is basically a Y-combinator although I think technically not quite). It's a journey that's taken me to, among many others, this thread and this thread and this thread.
I've boiled down one of my issues as cleanly as I can: how do I pass around templated functors which take lambdas as their template parameters?
...ANSWER
Answered 2021-Feb-05 at 18:54The only way I see is make evaluate()
a template method; if you want to be sure to receive a Functor
(but you can simply accept a callable: see Yakk's answer):
QUESTION
I'm doing some experimenting with y-combinator-like lambda wrapping (although they're not actually strictly-speaking y-combinators, I know), and I've encountered a very odd problem. My code operates exactly as I'd anticipate in a Debug configuration (with Optimizations turned off), but skips large (and important!) bits in Release (with it set to Optimizations (Favor Speed) (/Ox)
).
Please note, the insides of the lambda functions are basically irrelevant, they're just to be sure that it can recursion correctly etc.
...ANSWER
Answered 2021-Feb-04 at 18:14Undefined behavior is a non-localized phenomenon. If your program encounters UB, that means the program's behavior in its entirety is undefined, not merely that little part of it that did the bad thing.
As such, it is possible for UB to "time travel", affecting code that theoretically should have executed correctly before doing UB. That is, there is no "correctly" in a program that exhibits UB; either the program is correct, or it is incorrect.
How far that goes depends on the implementation, but as far as the standard is concerned, VS's behavior is consistent with the standard.
QUESTION
Note: this is a bonus for homework, but I have spent way too long on trying things to no avail. Help is much appreciated, but not necessary I suppose.
Premise:
generate a powerset for a list of numbers, but without using any helpers, explicit recursion, looping, or functions/constants other than cons
, first
, rest
, empty?
, empty
, else
, lambda
, and cond
, while using only one define
on the language level Intermediate Student with Lambda
. The order of the powerset does not matter.
What I've tried so far:
I have discovered the Y-combinator and anonymous recursion thanks to this post (author has the same end goal but we have different approaches, so the information in his post does not solve my problem), and the powerset
code in this answer, and with that I have written the following:
ANSWER
Answered 2020-Nov-19 at 03:44the Y-combinator only works with one parameter and combine needs 2
Any multi-argument function can be imagined as a one-argument function, returning a lambda that waits for the next argument. This process is called currying. For example, if we have
QUESTION
We can define a recursive function, factorial
as an example, by YCombinator
as follows
ANSWER
Answered 2020-May-12 at 19:48You say you understand this definition:
QUESTION
It is a well-known fact that OCaml rejects to define bare recursive types such as type t = t -> int
and the Y-combinator example in Rosetta Code are not working as well.
However, recently I found that the little tweak of the recursive type definition like type t = A of (t -> int)
works well. The following code is some proof-of-concept works to check which one works well.
ANSWER
Answered 2020-May-03 at 14:13Early versions of OCaml happily accepted types such as type t = t -> int
. They even inferred them. The problem with that was that in most of the practical cases, such a type just masks a programming error. So by popular demand, they were disallowed, and you now need to need an explicit datatype. You can still get the old behaviour if you use the -rectypes
option with the compiler.
That was merely a pragmatic decision. There is no semantic problem with such types, at least not in a language like OCaml.
Data types do not need to have non-recursive constructors as "leaf" cases, as long as the type of at least one constructor includes values that do not require another value of the defined type.
For example,
QUESTION
I'd like to implement a self-reference / pointer in Elm.
Specific Problem:I'm writing a toy LISP interpreter in Elm inspired by mal.
I'm attempting to implement something like letrec to support recursive and mutually-recursive bindings (the "self reference" and "pointers" I'm mentioning above).
Here's some example code:
...ANSWER
Answered 2020-Feb-29 at 06:02A binding construct in which the expressions can see the bindings doesn't require any exotic self-reference mechanisms.
How it works is that an environment is created for the variables, and then the values are assigned to them. The initializing expressions are evaluated in the environment in which those variables are already visible. Thus if those expressions happen to be lambda
expressions, then they capture that environment, and that's how the functions can refer to each other.
An interpreter does this by extending the environment with the new variables, and then using the extended environment for evaluating the assignments. Similarly, a compiler extends the compile-time lexical environment, and then compiles the assignments under that environment, so the running code will store values into the correct frame locations. If you have working lexical closures, the correct behavior of functions being able to mutually recurse just pops out.
Note that if the assignments are performed in left to right order, and one of the lambdas happens to be dispatched during initialization, and then happens to make a forward call to one of lambdas through a not-yet-assigned variable, that will be a problem; e.g.
QUESTION
I tried using the y-combinator (in both Lua and Clojure) as I thought that would allow me to exceed the size of default stack implementations when using recursion. It seems I was mistaken. Yes, it works, but in both of these systems, the stack blows at exactly the same point as it does using plain old recursion. A lowish ~3600 in Clojure and a highish ~333000 on my Android Lua implementation. It is also a bit slower than regular recursion.
So is there anything to be gained by using the y-combinator, or is it just an intellectual exercise to prove a point? Have I missed something?
===
PS. Sorry, I should have made it clearer that I am aware I can use TCO to exceed the stack. My question does not relate to that. I am interested in this a) from the academic/intellectual point of view b) whether there is anything that can be done about those function that cannot be written tail recursively.
...ANSWER
Answered 2018-Jun-20 at 02:57A “tail call” will allow you to exceed any stack size limitations. See Programming in Lua, section 6.3: Proper Tail Calls:
...after the tail call, the program does not need to keep any information about the calling function in the stack. Some language implementations, such as the Lua interpreter, take advantage of this fact and actually do not use any extra stack space when doing a tail call. We say that those implementations support proper tail calls.
Community Discussions, Code Snippets contain sources that include Stack Exchange Network
Vulnerabilities
No vulnerabilities reported
Install Y-Combinator
Support
Reuse Trending Solutions
Find, review, and download reusable Libraries, Code Snippets, Cloud APIs from over 650 million Knowledge Items
Find more librariesStay Updated
Subscribe to our newsletter for trending solutions and developer bootcamps
Share this Page