简体   繁体   中英

Is the expression (_, 'b') in Normal Form? in Weak Head Normal Form?

Working through an exercise in a haskell programming book, I am not sure of the answer and want to check my understanding.

The expression is: (_, 'b') .

Is this in Normal Form, in Weak Head Normal Form, or neither?


Here is a screenshot of the problem (#7) as listed in the book.

在此处输入图片说明

Unfortunately, the author has made a bad decision to use a _ character in these questions. As @Alec notes, the string of code (_,'b') is not a valid Haskell expression (because _ is a reserved identifier that can only appear in a pattern), so asking if (_,'b') is in normal form (NF) or weak head normal form (WHNF) is a bit like asking if an elephant is in NF or WHNF. It doesn't really make sense.

If the question had instead asked about (x, 'b') , that would have been a better question. This is a valid Haskell expression. We can tell it's in WHNF because its "head" (the outermost part of the expression) is a data constructor. In this case, it's the tuple constructor (,) which has a special syntax, so that makes it a little less obvious than usual, but note that (x, 'b') can be rewritten (,) x 'b' which makes it clear that the "head" is the data constructor (,) . It can't be in NF, though, because x isn't fully evaluated (ie, hasn't been replaced with its value, which is presumably given elsewhere).

In contrast, (1, 'b') would be in both WHNF (because it's head is the data constructor (,) ) and in NF because that data constructor is fully applied to arguments and each of those arguments have been fully evaluated.

Now, I think that what the author intended was for _ to stand for some unspecified, unevaluated part of an otherwise valid expression, so he or she intended you to reason that (_, 'b') is in WHNF (because the head is the data constructor (,) ) but not in NF (because there's an unevaluated portion _ floating around). So, the "right" answer is "WHNF only".

Does that help?

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM