我想知道是否有一个习惯用法来处理以下示例。它必须非常常见,而且我认为您不能使用 where 子句来处理它。除了写出两个单独的案例之外,我不知道该怎么做。
一般问题是我想根据 SelectionType
创建具有不同值的 map ,如下所示:
type OtherType = { x :: Int, y :: Int, z :: String } deriving (Show)
data SelectionType = A | B
myMapper = map foo someMyTypes
其中 someMyTypes::[MyType]
和 foo
是
foo :: SelectionType -> MyType -> OtherType
foo sel t = [ x t, y t, z t <> something ]
where
case SelectionType of
A -> something = "a selected"
B -> something = "b selected"
处理上述情况的最佳方法是什么,因为上面的内容无法编译。
最佳答案
只需将方程移到案例之外即可。
foo sel t = [ x t, y t, z t <> something ]
where
something = case sel of
A -> "a selected"
B -> "b selected"
关于Haskell,Where 内的案例,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/41689315/