Haskell Monad bind operator confusion

后端 未结 3 1028
滥情空心
滥情空心 2021-01-30 16:53

Okay, so I am not a Haskell programmer, but I am absolutely intrigued by a lot of the ideas behind Haskell and am looking into learning it. But I\'m stuck at square one: I can\'

3条回答
  •  小鲜肉
    小鲜肉 (楼主)
    2021-01-30 17:21

    You are not making a mistake. The key idea to understand here is currying - that a Haskell function of two arguments can be seen in two ways. The first is as simply a function of two arguments. If you have, for example, (+), this is usually seen as taking two arguments and adding them. The other way to see it is as a addition machine producer. (+) is a function that takes a number, say x, and makes a function that will add x.

    (+) x = \y -> x + y
    (+) x y = (\y -> x + y) y = x + y
    

    When dealing with monads, sometimes it is probably better, as ephemient mentioned above, to think of =<<, the flipped version of >>=. There are two ways to look at this:

    (=<<) :: (a -> m b) -> m a -> m b
    

    which is a function of two arguments, and

    (=<<) :: (a -> m b) -> (m a -> m b)
    

    which transforms the input function to an easily composed version as the article mentioned. These are equivalent just like (+) as I explained before.

提交回复
热议问题