Questions tagged [anonymous-recursion]

Anonymous-recursion is recursion which does not explicitly call a function by name. This is usually done by calling the current function from within itself or by passing the current function as a callback to an other (named) function. Use this tag for questions directly related to this type of recursion only.

17 questions
26
votes
9 answers

In Scheme, how do you use lambda to create a recursive function?

I'm in a Scheme class and I was curious about writing a recursive function without using define. The main problem, of course, is that you cannot call a function within itself if it doesn't have a name. I did find this example: It's a factorial…
NcAdams
  • 2,063
  • 3
  • 19
  • 31
8
votes
3 answers

Y combinator, Infinite types and Anonymous recursion in Haskell

I was trying to solve the maximal subsequence sum problem and came up with a neato solution msss :: (Ord a, Num a) => [a] -> a msss = f 0 0 f gmax _ [] = gmax f gmax lmax (x:xs) = let g = max (lmax + x) in f (g gmax) (g 0) xs You call the…
6
votes
7 answers

Which programming languages support functions that take themselves as arguments?

I'm doing an academic exercise (for personal growth). I want to find programming languages that allow you to define functions that are capable of accepting themselves (i.e., pointers to themselves) as arguments. For example, in JavaScript: function…
6
votes
1 answer

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

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,…
danfuzz
  • 4,123
  • 20
  • 34
5
votes
5 answers

Does "Anonymous Recursion" work in .NET? It does in Mono

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 fib = n => n > 1 ? fib(n - 1) + fib(n - 2) : n; The article then goes into some detail…
Justin
  • 8,323
  • 4
  • 35
  • 41
5
votes
2 answers

The mechanism of anonymous function to call itself in Scheme?

I'm reading The Little Schemer and feel confused about the following code: ((lambda (len) (lambda (l) (cond ((null? l) 0) (else (+ 1 (len (cdr l))))))) eternity) (define eternity (lambda (x) …
liu
  • 887
  • 1
  • 8
  • 15
3
votes
2 answers

List function with Y combinator does no recursion, why?

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…
David
  • 113
  • 2
  • 8
3
votes
3 answers

Generating powerset in one function, no explicit recursion, and using only simplest primitives in Racket

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…
user12781491
3
votes
1 answer

Howto: "letrec" in C# (lambda expression call within its definition)

Consider the factorial function defined within a method body as a lambda expression and assigned to a variable: Func factfail = n => { if (n == 0) return 1; else return n * factfail(n-1); }; This fails, since…
Sebastian Gregor
  • 345
  • 2
  • 11
3
votes
2 answers

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

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) …
2
votes
2 answers

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

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…
user8554766
2
votes
1 answer

How to do this length≤1 more than once?

I've spent a day reading page 166's length≤1 in the book The Little Schemer; there's the following code: (((lambda (mk-length) (mk-length mk-length)) (lambda (mk-length) (lambda (l) (cond ((null? l) 0) (else (add1 …
1
vote
4 answers

Recursion with anonymous function

Possible Duplicates: javascript: recursive anonymous function? Anonymous recursive PHP functions I was wondering... Is it possible to do recursion with anonymous function? Here is one example: I need to get six-chars long string which may contain…
daGrevis
  • 19,600
  • 35
  • 95
  • 134
1
vote
1 answer

Call By Need in Scheme

I have this code knowing that parameters are passed using call by need: (define fact-2 (let ((foo (lambda (n f) (if (zero? n) 1 (f n f))))) (lambda (n) (let ((res 1)) (foo n…
1
vote
1 answer

C# anonymous recursion and Y-combinator performance

Below are are functions and tests of anonymous recursion. The first one is true Y-combinator, looks fine and simple, but is quite slow. It takes 1000ms to execute 1 mln iterations. The second is quite ugly because of c(c,item) but works twice faster…
user448516
1
2