Elegant Haskell case/error handling

后端 未结 2 742
悲哀的现实
悲哀的现实 2021-01-13 18:40

I\'m trying to better understand how to handle error cases in haskell and made wrote some code to help me with that.

Is there a better way (more elegant, sho

2条回答
  •  陌清茗
    陌清茗 (楼主)
    2021-01-13 19:16

    I think the most readable way is the maybe function you already tried, just a little bit prettified by avoiding lambdas (pointfree style) and using maybe even for the innermost check (by replacing if with mfilter):

    import Control.Monad(mfilter)
    
    process2 :: MyType -> String
    process2 =  
      maybe "error"  $ 
      maybe "error2" $ 
      maybe "error3" 
      show . mfilter (<10) . Just 
    

提交回复
热议问题