y-combinator

Why is the type of this function (a -> a) -> a?

青春壹個敷衍的年華 提交于 2019-12-12 10:29:05
问题 Why is the type of this function (a -> a) -> a? Prelude> let y f = f (y f) Prelude> :t y y :: (t -> t) -> t Shouldn't it be an infinite/recursive type? I was going to try and put into words what I think it's type should be, but I just can't do it for some reason. y :: (t -> t) -> ?WTFIsGoingOnOnTheRHS? I don't get how f (y f) resolves to a value. The following makes a little more sense to me: Prelude> let y f x = f (y f) x Prelude> :t y y :: ((a -> b) -> a -> b) -> a -> b But it's still

Finite number of recursions in Javascript with ES6 Y-combinator

别来无恙 提交于 2019-12-12 08:59:44
问题 I came across an answer to another SO question about recursion in Javascript, that gave a very terse form in ES6 using the Y-combinator, using ES6's fat arrow, and thought hey, that'd be neat to use - then 15 minutes or so later had circled back to hm, maybe not . I've been to a few Haskell/Idris talks and run some code before, and familiar with standard JS, so was hoping I'd be able to figure this out, but can't quite see how a simple "do n recursions and return" is supposed to go, and where

How to implement iteration of lambda calculus using scheme lisp?

我的未来我决定 提交于 2019-12-11 08:04:38
问题 I'm trying to learn lambda calculus and Scheme Lisp. The tutorial on lambda calculus can be found here http://www.inf.fu-berlin.de/lehre/WS03/alpi/lambda.pdf. The problem I'm facing is I don't know how to properly implement iteration. (define (Y y) (((lambda (x) (y (x x))) (lambda (x) (y (x x)))))) (define (sum r n) ((is_zero n) n0 (n succ (r (pred n))))) (display ((Y sum) n5)) I always get this error: Aborting!: maximum recursion depth exceeded I know the problem is about the evaluation

Unable to get implementation of Y combinator working

你说的曾经没有我的故事 提交于 2019-12-11 07:56:53
问题 Here's the code (also here): #lang racket (define poorY ((lambda length (lambda (ls) (cond [(null? ls) 0] [else (add1 ((length length) (cdr ls)))]))) (lambda length (lambda (ls) (cond [(null? ls) 0] [else (add1 ((length length) (cdr ls)))]))))) When I run it: > (poorY '(9 7 8)) . . application: not a procedure; expected a procedure that can be applied to arguments given: '(#<procedure>) arguments...: '(#<procedure>) The screenshot looks like this: I'm using DrRacket as the repl. What's wrong

Does “Anonymous Recursion” work in .NET? It does in Mono

爷,独闯天下 提交于 2019-12-10 14:09:49
问题 I surfed into this site a few days ago on "Anonymous Recursion in C#". The thrust of the article is that the following code will not work in C#: Func<int, int> fib = n => n > 1 ? fib(n - 1) + fib(n - 2) : n; The article then goes into some detail about how to use currying and the Y-combinator to get back to "Anonymous Recursion" in C#. This is pretty interesting but a tad complex for my everyday coding I am afraid. At this point at least... I like to see stuff for myself so I opened the Mono

Little Schemer: why wrap (mk-length mk-length) into a function?

Deadly 提交于 2019-12-10 10:47:55
问题 In The Little Schemer book, in Chapter 9, while building a length function for arbitrary long input, the following is suggested (on pages 170-171), that in the following code snippet (from page 168 itself): ((lambda (mk-length) (mk-length mk-length)) (lambda (mk-length) ((lambda (length) (lambda (l) (cond ((null? l) 0) (else (add1 (length (cdr l))))))) (mk-length mk-length)))) the part (mk-length mk-length) , will never return and will be infinitely applying itself to itself: Because we just

fixed point combinator in lisp

纵饮孤独 提交于 2019-12-07 09:10:57
问题 ;; compute the max of a list of integers (define Y (lambda (w) ((lambda (f) (f f)) (lambda (f) (w (lambda (x) ((f f) x))))))) ((Y (lambda (max) (lambda (l) (cond ((null? l) -1) ((> (car l) (max (cdr l))) (car l)) (else (max (cdr l))))))) '(1 2 3 4 5)) I wish to understand this construction. Can somebody give a clear and simple explanation for this code? For example, supposing that I forget the formula of Y. How can I remember it , and reproduce it long after I work with it ? 回答1: Here's some

Y Combinator implementation Scheme

和自甴很熟 提交于 2019-12-07 07:58:33
问题 I am really new to scheme functional programming. I recently came across Y-combinator function in lambda calculus, something like this Y ≡ (λy.(λx.y(xx))(λx.y(xx))) . I wanted to implement it in scheme, i searched alot but i didn't find any implementation which exactly matches the above given structure. Some of them i found are given below: (define Y (lambda (X) ((lambda (procedure) (X (lambda (arg) ((procedure procedure) arg)))) (lambda (procedure) (X (lambda (arg) ((procedure procedure) arg

Two-layer “Y-style” combinator. Is this common? Does this have an official name?

情到浓时终转凉″ 提交于 2019-12-07 03:56:15
问题 I've been looking into how languages that forbid use-before-def and don't have mutable cells (no set! or setq ) can nonetheless provide recursion. I of course ran across the (famous? infamous?) Y combinator and friends, e.g.: http://www.ece.uc.edu/~franco/C511/html/Scheme/ycomb.html http://okmij.org/ftp/Computation/fixed-point-combinators.html http://www.angelfire.com/tx4/cus/combinator/birds.html http://en.wikipedia.org/wiki/Fixed-point_combinator When I went to implement "letrec" semantics

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

强颜欢笑 提交于 2019-12-06 02:49:55
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. (mk-length mk-length)) (lambda (mk-length) (lambda (l) (cond ((null? l) 0 ) (else (add1((mk-length mk