y-combinator

List function with Y combinator does no recursion, why?

耗尽温柔 提交于 2020-12-31 07:00:17
问题 Note: This is kind of homework, kind of not -- the end goal is to have a function that produces a powerset of a set of numbers supplied to the function as a list of numbers. I have a recursive version of the function but I now need to find some ways of replacing each explicitly recursive function in the solution I have ( append , mapm etc.) with an equivalent lambda-only expression. As such, I am starting with smaller problems and hope to combine them all to write a full function. I've

List function with Y combinator does no recursion, why?

半世苍凉 提交于 2020-12-31 06:56:34
问题 Note: This is kind of homework, kind of not -- the end goal is to have a function that produces a powerset of a set of numbers supplied to the function as a list of numbers. I have a recursive version of the function but I now need to find some ways of replacing each explicitly recursive function in the solution I have ( append , mapm etc.) with an equivalent lambda-only expression. As such, I am starting with smaller problems and hope to combine them all to write a full function. I've

(self self) call inside the let statement, in strict language

十年热恋 提交于 2020-01-25 00:48:11
问题 I am currently, going through this article on Y-combinator by Mike Vanier. Along the way of Y-combinator derivation, this code: (define (part-factorial self) (lambda (n) (if (= n 0) 1 (* n ((self self) (- n 1)))))) ((part-factorial part-factorial) 5) ==> 120 (define factorial (part-factorial part-factorial)) (factorial 5) ==> 120 is worked out to: (define (part-factorial self) (let ((f (self self))) (lambda (n) (if (= n 0) 1 (* n (f (- n 1))))))) (define factorial (part-factorial part

Explain this implementation of the Y combinator in Scala?

最后都变了- 提交于 2020-01-11 19:59:26
问题 This is a implementation of the Y-combinator in Scala: scala> def Y[T](func: (T => T) => (T => T)): (T => T) = func(Y(func))(_:T) Y: [T](func: (T => T) => (T => T))T => T scala> def fact = Y { | f: (Int => Int) => | n: Int => | if(n <= 0) 1 | else n * f(n - 1)} fact: Int => Int scala> println(fact(5)) 120 Q1: How does the result 120 come out, step by step? Because the Y(func) is defined as func(Y(func)) , the Y should become more and more,Where is the Y gone lost and how is the 120 come out

How does Y-combinator compute the fixed point programmatically?

我只是一个虾纸丫 提交于 2019-12-29 06:53:26
问题 I believe I understand mathematically the idea of Y-combinator: it returns the fixed point of a given functional F , thus f = Y(F) where f satisfies f == F(f) . But I don't understand how it does the actual computation program wise? Let's take the javascript example given here: var Y = (F) => ( x => F( y => x(x)(y) ) )( x => F( y => x(x)(y) ) ) var Factorial = (factorial) => (n => n == 0 ? 1 : n * factorial(n-1)) Y(Factorial)(6) == 720 // => true computed_factorial = Y(Factorial) The part I

Understanding Y Combinator through generic lambdas

不羁岁月 提交于 2019-12-28 16:09:42
问题 While building a small lambda-based metaprogramming library, I had the necessity of using recursion in a C++14 generic lambda, to implement a left-fold. My own solution was passing the lambda itself as one of its parameters, like this: template <typename TAcc, typename TF, typename... Ts> constexpr auto fold_l_impl(TAcc acc, TF f, Ts... xs) { // Folding step. auto step([=](auto self) { return [=](auto y_acc, auto y_x, auto... y_xs) { // Compute next folding step. auto next(f(y_acc, y_x)); //

y-combinator in javascript

心已入冬 提交于 2019-12-25 09:04:40
问题 I have built a y-combinator in js like this const y = f => { const g = self => x => f(self(self))(x); return g(g);} and I simplified this code like this const y = f => { const g = self => f(self(self)); return g(g);} this get an infinite recursion. What's the difference between these two versions? 回答1: If you don't understand the difference between the two, I would be surprised that you actually built them. Nevertheless, perhaps the best way to demonstrate the difference between the two, is

Little Schemer: write function that only supports lists of length ≤ 2

扶醉桌前 提交于 2019-12-22 10:46:39
问题 In the book The little schemer , we find this function that only supports lists with length smaller than or equal to 1 : (((lambda (mk-length) ; A. (mk-length mk-length)) (lambda (mk-length) (lambda (l) (cond ((null? l ) 0) (else (add1 ((mk-length eternity ) (cdr l)))))))) '(1)) I want to study step by step, and want to write the similar function that supports only lists of length smaller than or equal to 2 . Please, don't answer this question by offering code like: (((lambda (mk-length) ; B.

YCombinator not working in Swift

99封情书 提交于 2019-12-21 05:37:08
问题 I am trying to create a lambda function as such to get a factorial function but this throws a segmentation fault and errors out. How do I get this working in Swift. Please look at this video for reference on what I am trying to do http://www.confreaks.com/videos/1287-rubyconf2012-y-not-adventures-in-functional-programming typealias f = () -> () typealias g = (Int) -> (Int) typealias F = Any -> g let y = { (gen: Any) -> g in (gen as F)(gen) } let fact = y({ (gen: Any) -> g in { (n: Int) -> Int

Have I implemented Y-combinator using C# dynamic, and if I haven't, what is it?

徘徊边缘 提交于 2019-12-18 02:28:26
问题 My brain seems to be in masochistic mode, so after being drowned in this, this and this, it wanted to mess around with some DIY in C#. I came up with the following, which I don't think is the Y-combinator, but it does seem to manage to make a non-recursive function recursive, without referring to itself: Func<Func<dynamic, dynamic>, Func<dynamic, dynamic>> Y = x => x(x); So given these: Func<dynamic, Func<dynamic, dynamic>> fact = self => n => n == 0 ? 1 : n * self(self)(n - 1); Func<dynamic,