求值策略
閱讀設定
求值策略(英文:evaluation strategy)係指一隻程式語言用咩方法嚟同啲表達式做求值。
主要策略
[編輯]舉例說明,想像家陣要行以下噉嘅表達式[1]
def double x = (plus x x) def average x y = (divide (plus x y) 2)
用正常次序求值(normal-order evaluation)嘅話,會做嘅嘢係「吓吓都郁手拆咗最左嗰個子程序先」→
(double (average 2 4)) => (plus (average 2 4) (average 2 4)) => (plus (divide (plus 2 4) 2) (average 2 4)) => ...
而用應用次序求值(applicative-order evaluation)嘅話,會做嘅嘢係「吓吓都郁手拆咗最入嗰個子程序先」→
double (average 2 4) => double (divide (plus 2 4) 2) => double (divide 6 2) => ...
唔同嘅求值策略都有各自嘅利弊,同一條表達式,有可能用一種求值策略會攞到正確答案,但用另一種求值策略就會搞到輕機[2]。
睇埋
[編輯]攷
[編輯]- ↑ Abelson, Harold; Sussman, Gerald Jay (1996). Structure and Interpretation of Computer Programs (Second ed.). Cambridge, Massachusetts: The MIT Press. p. 21-22.
- ↑ Normal, Applicative and Lazy Evaluation. Kevin Sookocheff.