Warning: file_get_contents(/data/phpspider/zhask/data//catemap/6/haskell/8.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Haskell 跨保护模式共享定义_Haskell_Pattern Matching_Helper - Fatal编程技术网

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+)。