简体   繁体   中英

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. 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:

type OtherType = { x :: Int, y :: Int, z :: String } deriving (Show)

data SelectionType = A | B

myMapper = map foo someMyTypes

where someMyTypes :: [MyType] and foo is

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"

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM