[英]Haskell, Case inside of Where
I'm wondering if there is an idiom to handle the following example. 我想知道是否有处理以下示例的惯用法。 It has to be pretty common, and I don't think you can use where clauses to handle it. 它必须很常见,我认为您不能使用where子句来处理它。 Other than writing out two separate cases, I don't know how I would do it. 除了写出两个单独的案例,我不知道该怎么做。
The general problem is that I want to create a map with a different value depending on a SelectionType
, like so: 一般的问题是我想根据SelectionType
创建一个具有不同值的地图,如下所示:
type OtherType = { x :: Int, y :: Int, z :: String } deriving (Show)
data SelectionType = A | B
myMapper = map foo someMyTypes
where someMyTypes :: [MyType]
and foo
is 其中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"
What is the best way to handle this above case, since the above doesn't compile. 由于上述内容无法编译,因此处理上述情况的最佳方法是什么。
Just move the equation outside of the case. 只需将方程式移出案例即可。
foo sel t = [ x t, y t, z t <> something ]
where
something = case sel of
A -> "a selected"
B -> "b selected"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.