Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

Tags:

case

haskell

I have a lot of code of the style:

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

Any way of me combining the "x <- ..." and "case x of" lines to eliminate the need for a variable?

like image 854
qrest Avatar asked Aug 04 '10 06:08

qrest


1 Answers

You could use the bind operator >>= to pipe the 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"
like image 184
Ionuț G. Stan Avatar answered Oct 06 '22 01:10

Ionuț G. Stan