Hacker News new | past | comments | ask | show | jobs | submit login

There are two ways of recursively calling lambda functions, neither perfect, but both usable.

  Func<int,int> fib = null;
  fib = n => n > 1 ? fib(n - 1) + fib(n - 2) : n;
Or write yourself a Y fixed-point combinator, described in http://blogs.msdn.com/b/wesdyer/archive/2007/02/02/anonymous...

  delegate Func<A,R> Recursive<A,R>(Recursive<A,R> r);
  static Func<A, R> Y<A, R>(Func<Func<A, R>, Func<A, R>> f) {
    Recursive<A, R> rec = r => a => f(r(r))(a);
    return rec(rec);
  }
used like this:

  Func<int,int> fib = Y<int,int>(f => n => n > 1 ? f(n - 1) + f(n - 2) : n);
(I'm not advocating writing a fib function this way, but it's a useful example!)



Guidelines | FAQ | Support | API | Security | Lists | Bookmarklet | Legal | Apply to YC | Contact

Search: