How does one implement recursion with a function expression {code? The {code...} clause is not very elegant. {code...} has a function expression.
$make = function($std){};
How to Implement recursion?
$make = function($std){ $make($std); };
It is not very elegant to exclude such writing.
$make = function($std, $make){ $make($std); };$make($std, $make);
Reply content:
There is a function expression
$make = function($std){};
How to Implement recursion?
$make = function($std){ $make($std); };
It is not very elegant to exclude such writing.
$make = function($std, $make){ $make($std); };$make($std, $make);
$f = function($x) use(&$f){ if($x==0 || $x==1) return 1; return $f($x-1) + $f($x-2);};print $f(10);
I think the main idea must be something like this. Note,use(&$f)
References must be used here; otherwise, errors may occur.
Use can beWhen defining anonymous FunctionsTo capture external variables (copying values) into an anonymous function.
In this example, $ f can be considered as nonexistent when the assignment is not completed. In this case, an undefined variable is captured. Therefore, we need to capture a reference. After the $ f value is assigned, $ f inside the anonymous function points to the anonymous function itself.
(This is too difficult ...)
This Fibonacci recursive computation is a reference for you.