Data commit
This commit is contained in:
parent
7387c8f97b
commit
cb5bb5e222
199093 changed files with 3378972 additions and 0 deletions
16
Task/Anonymous-recursion/PHP/anonymous-recursion-1.php
Normal file
16
Task/Anonymous-recursion/PHP/anonymous-recursion-1.php
Normal file
|
|
@ -0,0 +1,16 @@
|
|||
<?php
|
||||
function fib($n) {
|
||||
if ($n < 0)
|
||||
throw new Exception('Negative numbers not allowed');
|
||||
$f = function($n) { // This function must be called using call_user_func() only
|
||||
if ($n < 2)
|
||||
return 1;
|
||||
else {
|
||||
$g = debug_backtrace()[1]['args'][0];
|
||||
return call_user_func($g, $n-1) + call_user_func($g, $n-2);
|
||||
}
|
||||
};
|
||||
return call_user_func($f, $n);
|
||||
}
|
||||
echo fib(8), "\n";
|
||||
?>
|
||||
14
Task/Anonymous-recursion/PHP/anonymous-recursion-2.php
Normal file
14
Task/Anonymous-recursion/PHP/anonymous-recursion-2.php
Normal file
|
|
@ -0,0 +1,14 @@
|
|||
<?php
|
||||
function fib($n) {
|
||||
if ($n < 0)
|
||||
throw new Exception('Negative numbers not allowed');
|
||||
$f = function($n) use (&$f) {
|
||||
if ($n < 2)
|
||||
return 1;
|
||||
else
|
||||
return $f($n-1) + $f($n-2);
|
||||
};
|
||||
return $f($n);
|
||||
}
|
||||
echo fib(8), "\n";
|
||||
?>
|
||||
18
Task/Anonymous-recursion/PHP/anonymous-recursion-3.php
Normal file
18
Task/Anonymous-recursion/PHP/anonymous-recursion-3.php
Normal file
|
|
@ -0,0 +1,18 @@
|
|||
<?php
|
||||
class fib_helper {
|
||||
function __invoke($n) {
|
||||
if ($n < 2)
|
||||
return 1;
|
||||
else
|
||||
return $this($n-1) + $this($n-2);
|
||||
}
|
||||
}
|
||||
|
||||
function fib($n) {
|
||||
if ($n < 0)
|
||||
throw new Exception('Negative numbers not allowed');
|
||||
$f = new fib_helper();
|
||||
return $f($n);
|
||||
}
|
||||
echo fib(8), "\n";
|
||||
?>
|
||||
Loading…
Add table
Add a link
Reference in a new issue