1
我看SICP 2A講座:無法實現SICP平均DAMP在PHP
https://www.youtube.com/watch?v=erHp3r6PbJk&list=PL8FE88AA54363BC46
周圍32:30傑拉德·傑伊·薩斯曼介紹AVERAGE受潮程序。它接受一個過程並返回一個過程,返回它的參數的平均值以及應用於參數的第一個過程。在計劃它看起來像這樣:
(define average-damp
(lambda (f)
(lambda (x) (average (f x) x))))
我決定重寫它在PHP:
function average_damp($f)
{
return function ($x)
{
$y = $f($x);//here comes the error - $f undefined
return ($x + $y)/2;
};
}
,然後用簡單的程序審理:
function div_by2($x)
{
return $x/2;
}
$fun = average_damp("div_by2");
$fun(2);
這東西應該返回的平均在2和(2/2)=(2 + 1)/ 2 = 3/2之間。
但是$ F未定義的內部程序,給了一個錯誤:
PHP Notice: Undefined variable: f on line 81
PHP Fatal error: Function name must be a string on line 81
如何解決?