user7428320
user7428320

Reputation: 43

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.

Upvotes: 2

Views: 3074

Answers (1)

Daniel Wagner
Daniel Wagner

Reputation: 152682

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"

Upvotes: 4

Related Questions