Haskell 跨保护模式共享定义
假设我有一个函数:Haskell 跨保护模式共享定义,haskell,pattern-matching,helper,Haskell,Pattern Matching,Helper,假设我有一个函数: arbitrary :: String -> String -> Maybe String arbitrary st1 st2 | (st1 == st2) = Just "foo" | (arbitrarily_complex_calculation == 7) = Nothing | otherwise = Just $ show arbitrarily_complex_calculati
arbitrary :: String -> String -> Maybe String
arbitrary st1 st2 | (st1 == st2) = Just "foo"
| (arbitrarily_complex_calculation == 7) = Nothing
| otherwise = Just $ show arbitrarily_complex_calculation
如何在两个保护块之间共享任意复杂的_计算?这可以通过
let
/where
来完成吗?或者我必须编写一个helper函数吗?是的,where
子句是有效的(并且经常使用),带有防护:
arbitrary :: String -> String -> Maybe String
arbitrary st1 st2
| st1 == st2 = Just "foo"
| acc == 7 = Nothing
| otherwise = Just $ show acc
where
acc = arbitrarily_complex_calculation
而没有“哪里”?CSE仍然可以共享。@d8d0d65b3f7cf42,除非
任意复杂计算
具有多态类型(但这会干扰显示
实例)。此外,GHC(但我不知道这是否仍然适用于GHC 7.10+)。