Haskell version of Idris !-notation (bang notation)

前端 未结 3 540
伪装坚强ぢ
伪装坚强ぢ 2021-02-13 21:04

I\'ve had the luxury of learning a bit of Idris lately and one thing I\'ve found extremely convenient is the !-notation, which let\'s me shorten monadic code inside a do block s

相关标签:
3条回答
  • 2021-02-13 21:44

    An alternative to @chi's answer is liftA3 someFunction a b c (with join if needed).

    0 讨论(0)
  • 2021-02-13 21:47

    Since every monad is an Applicative (with GHC >= 7.10) we can write

    someFunction <$> a <*> b <*> c
    

    Note that if someFunction returns a monadic value of type m T, the above will return m (m T), which is likely not what we want (as @pigworker points out below). We can however join the two layers together:

    join $ someFunction <$> a <*> b <*> c
    
    0 讨论(0)
  • 2021-02-13 22:09

    Speaking of arrows...

    import Control.Arrow
    
    a' = Kleisli $ const a
    b' = Kleisli $ const b
    c' = Kleisli $ const c
    
    foo = (`runKleisli`()) $
      (a' &&& b') &&& c' >>> uncurry (uncurry someFunction)
    

    Not that I recommend this.

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