> =", I have a lot of style code: do x <- getSomething case x of this -> ... that -> ... other -> ... Any w...">

In haskell combining "case" and ">> =",

I have a lot of style code:

do x <- getSomething case x of this -> ... that -> ... other -> ... 

Any way to combine the strings "x <-..." and "case x" to eliminate the need for a variable?

+7
haskell case
source share
3 answers

You can use the binding operator >>= to connect x .

 import System.Environment (getArgs) main :: IO () main = getArgs >>= process where process ["xxx"] = putStrLn "You entered xxx" process ["yyy"] = putStrLn "You entered yyy" process _ = putStrLn "Error" 
+6
source share

I do it like

 foo "this" = return 2 foo "that" = return 3 main = foo =<< getSomething 

The best part about this approach is that if foo is clean, then it becomes

 main = foo <$> getSomething 

Thus, the code retains the same form for several different circumstances.

0
source share

If you want something very close to:

 getSomething >>= caseOf this -> expr1 that -> expr2 other -> expr3 

Then I think you're just out of luck - there is no such syntax in Haskell. But know that you are not alone. Mark Jones defined the Habit language to include a kind of monadic case with the syntax:

 case<- getSomething of Nothing -> expr1 Just x -> expr2 x 

He refers to this as the expression “Case-From” in a language definition.

0
source share

All Articles