[問題] pattern matching in Haskell

看板PLT (程式語言與理論)作者 (i'm only dust)時間14年前 (2010/07/07 19:45), 編輯推噓0(000)
留言0則, 0人參與, 最新討論串1/3 (看更多)
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
文章代碼(AID): #1CD6ZYrY (PLT)
文章代碼(AID): #1CD6ZYrY (PLT)