Weak head normal form and normal form
I have question about weak head normal form and normal form.
Weak head normal form means, the expression will only evaluate as far as necessary to reach to a data constructor.
Normal form means, the expression will be fully evaluated.
Now, I have following expression:
x -> x * 10
Why the expression above is in normal form?
"Papu" ++ "chon"
Why the expression above is in neither WHNF nor NF?
WHNF evaluates far enough to reach a data constructor or lambda function.
If you have a lambda function that hasn't been called with an argument, you cannot evaluate it any further anyway. So a lambda function is in WHNF, and indeed NF, since there is nothing to evaluate further. Now, if you call the lambda function with an argument, we can evaluate what the result might be. But the lambda function on its own? Nothing further to be done.
Your first expression is a lambda function with no arguments. It is therefor in normal form.
Your second expression is neither a data constructor nor a lambda, hence is not in any normal form. Now, if you evaluate it for one step, you obtain
'P' : ("apu" ++ "chon")
Which (although the syntax doesn't look like it) begins with a data constructor (namely (:)
), and hence is in WHNF, but not NF (since it still contains the unevaluated (++)
subexpression).
Perhaps it's easier if we get rid on the infix syntax:
(++) "Papu" "chon"
vs
(:) 'P' ( (++) "apu" "chon" )
x -> x * 10
Why the expression above is in normal form?
Because, you can't simplify it.
"Papu" ++ "chon"
Why the expression above is in neither WHNF nor NF?
First of all, if some expression in NF - that means it's in WHNF. NF it's more restricted version of WHNF.
So, We can restructure your question. Why the expression above isn't in WHNF?
Simple answer is: We can simplify it to "Papuchon"
.
If you want to know what difference between WHNF and NF, you can read, for example, this.
链接地址: http://www.djcxy.com/p/43008.html上一篇: Wadler,“用于函数式编程的Monads”,2.8节
下一篇: 弱头正常形式和正常形式