Fixed point combinator for mutually recursive functions?

后端 未结 3 1565
眼角桃花
眼角桃花 2021-01-02 01:38

Is there a fixed point combinator for creating tuples of mutually recursive functions? I.e. I\'m looking for something like the Y-Combinator but which takes multiple \"recu

3条回答
  •  别那么骄傲
    2021-01-02 02:26

    The following web page describes the fix-point combinators for mutual recursion (polyvariadic fixpoint combinators) in detail. It derives the simplest so far combinator. http://okmij.org/ftp/Computation/fixed-point-combinators.html#Poly-variadic

    For ease of reference, here is the simplest polyvariadic combinator in Haskell (one-liner)

    fix_poly:: [[a]->a] -> [a]
    fix_poly fl = fix (\self -> map ($ self) fl)
      where fix f = f (fix f)
    

    and here it is in Scheme, a strict language

     (define (Y* . l)
       ((lambda (u) (u u))
        (lambda (p)
           (map (lambda (li) (lambda x (apply (apply li (p p)) x))) l))))
    

    Please see the web page for examples and more discussion.

提交回复
热议问题