[問題] pattern matching in Haskell
hi,
i'm a novice, so better to say sorry first for anything stupid i'll ask.
and for some reason i can only type in english.
in chapter 6. of the book "Real World Haskell", the author explained some
features of "newtype".
data DataInt = D Int
deriving (Eq, Ord, Show)
newtype NewtypeInt = N Int
deriving (Eq, Ord, Show)
=====
1.
ghci> case undefined of D _ -> 1
*** Exception: Prelude.undefined
the author wrote:
"the unprotected undefined is evaluated when the pattern match occurs".
i realise that matching is based on value constructors.
but under what circumstances does it have to evaluate them?
in this case, does pattern matching run into a divergence?
("divergence", or diverge as verb is introduced in many documents. but i
can't make out.)
pattern matching, does it happen in runtime, compile time or both?
if it's a runtime process, then some sort of bookkeeping data about
constructors should be retained during programme execution, right?
=====
2.
ghci> case undefined of N _ -> 1
1
the author said:
"there's no constructor present at runtime, matching against N _ is
in fact equivalent to matching against the plain wild card (_)"
but something like
ghci> case 10.77 of N _ -> 1
ghci> case "a string" of N _ -> 1
justifiably fail
regarding to the author's explanation, why the two cases fail while
the "undefined" case succeeds?
aren't they also equivalent to matching against wild card (_)?
sorry to ask so many questions, i checked few documents but got stuck
every so often.
little complaint: "a gentle introduction to Haskell" is not gentle but
prickly to me :(
--
※ 發信站: 批踢踢實業坊(ptt.cc)
◆ From: 111.70.33.88
討論串 (同標題文章)
PLT 近期熱門文章
PTT數位生活區 即時熱門文章