我圖案上記錄的數據構造的匹配,我有下面的代碼塊:使用匹配的值構建在Haskell
colorFor shape =
case material shape of
ColorMaterial -> material shape
-- etc.
問題是這樣的:material
是一個非平凡方法,我不想在案件陳述中重新計算它。我知道我可以做這樣的事情:
colorFor shape =
let m = material shape
in case m of
ColorMaterial -> m
或
colorFor shape =
case material shape of
ColorMaterial r g b -> ColorMaterial r g b
但我忍不住想必須有某種方式來檢索模式匹配匹配的值。這也適用於函數定義,我想在數據構造函數中匹配某個參數而不完全解壓縮它。
僅供參考:我是Haskell的新手,如果有更好的方法來做我正在做的事情,我非常樂於提供建議。任何幫助非常感謝!
這裏的潛在問題是,你必須定義方式多個「模式」,一爲每次想立即返回,而不是一個讓數據類型。 – Phyx
s/datatype/pattern – Phyx
這確實是我正在尋找的東西,但Phyx關於它是如何被轉換爲'let'的說法已經說服我保持它原樣。謝謝! –