case on monadic value

后端 未结 3 1175
爱一瞬间的悲伤
爱一瞬间的悲伤 2021-02-04 02:10

Is there a way to perform a case on the value stored within a monad without having to bind a name to it?

i.e. instead of doing this:

c <- getChar
case         


        
相关标签:
3条回答
  • 2021-02-04 02:46

    No, not really, but you can move the case into another function and apply it to the result of a monadic action.

    f x = case  x of ...
    
    main = do
      f <$> getChar
    

    Alternativly, the following is possible:

    getChar >>= \x -> case x of ...
    
    0 讨论(0)
  • 2021-02-04 02:55

    The proposal mentioned by FUZxxl was now implemented in GHC since 7.6.1, it's called LambdaCase.

    Now you can do:

    {-# LANGUAGE LambdaCase #-}
    getChar >>= \case
       ...
    

    Note the \ before the case keyword and the fact that there is no of in that case.

    0 讨论(0)
  • 2021-02-04 03:00

    The answer is no. In Haskell 98, you can't use a case statement without using a name inside it. But there is a proposal for adding support for case-lambdas. The syntax they propose is the same you propose too.

    0 讨论(0)
提交回复
热议问题