2011-06-28 96 views
6

我明白,在:元組砰圖案

f x = x + 1 where !y = undefined

轟圖案的含義是,yf之前進行評估。

類似地:

f x = x + 1 where !(!a, !b) = (undefined, undefined)

的含義是一樣的,w.r.t xy

但爲什麼偏偏在一聲模式中的意思是:

f x = x + 1 where (!a, !b) = (undefined, undefined)

這似乎並沒有引起未定義進行評估。元組爆炸模式何時生效?如果模式的元組被強制?任何人都可以舉例(!a, !b) = (..)(a, b) = (..)不同嗎?

回答

9

元組本身的爆炸模式將強制評估元組而不是其元素。每當元組本身被評估時,元組元素上的Bang模式將強制它們。

這裏的不同行爲的一個例子:

Prelude> let x = a + 1 where (a, b) = (1, undefined) 
Prelude> x 
2 
Prelude> let x = a + 1 where (!a, !b) = (1, undefined) 
Prelude> x 
*** Exception: Prelude.undefined 
4

如果翻譯爲let

f x = let (!a, !b) = (undefined, undefined) in x + 1 

在這裏,你創建一個包含(a, b)一個元組,而當​​元組進行評估,既ab是。

但是因爲元組從未被評估過,所以ab都不是。這與寫作基本相同:

f x = let y = undefined `seq` 4 in x + 1 

由於永遠不會評估y,因此也不是undefined