Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>I know this might not be a simple approach, but I learned about a technique called <a href="http://en.wikibooks.org/wiki/Haskell/Fix_and_recursion" rel="noreferrer">"fix"</a> from functional languages. The <code>fix</code> function from Haskell is known more generally as the <a href="http://en.wikipedia.org/wiki/Fixed-point_combinator#Y_combinator" rel="noreferrer">Y combinator</a>, which is one of the most well-known <a href="http://en.wikipedia.org/wiki/Fixed-point_combinator" rel="noreferrer">fixed point combinators</a>. </p> <p>A fixed point is a value that is unchanged by a function: a fixed point of a function <em>f</em> is any <em>x</em> such that x&nbsp;=&nbsp;f(x). A fixed point combinator <em>y</em> is a function that returns a fixed point for any function f. Since y(f) is a fixed point of f, we have y(f)&nbsp;=&nbsp;f(y(f)).</p> <p>Essentially, the Y combinator creates a new function that takes all the arguments of the original, plus an additional argument that's the recursive function. How this works is more obvious using curried notation. Instead of writing arguments in parentheses (<code>f(x,y,...)</code>), write them after the function: <code>f x y ...</code>. The Y combinator is defined as <code>Y f = f (Y f)</code>; or, with a single argument for the recursed function, <code>Y f x = f (Y f) x</code>.</p> <p>Since PHP doesn't automatically <a href="http://www.haskell.org/haskellwiki/Currying" rel="noreferrer">curry</a> functions, it's a bit of a hack to make <code>fix</code> work, but I think it's interesting.</p> <pre><code>function fix( $func ) { return function() use ( $func ) { $args = func_get_args(); array_unshift( $args, fix($func) ); return call_user_func_array( $func, $args ); }; } $factorial = function( $func, $n ) { if ( $n == 1 ) return 1; return $func( $n - 1 ) * $n; }; $factorial = fix( $factorial ); print $factorial( 5 ); </code></pre> <p>Note this is almost the same as the simple closure solutions others have posted, but the function <code>fix</code> creates the closure for you. Fixed point combinators are slightly more complex than using a closure, but are more general, and have other uses. While the closure method is more suitable for PHP (which isn't a terribly functional language), the original problem is more of an exercise than for production, so the Y combinator is a viable approach.</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload