1

Haskell におけるモナドの概念、つまり >>= と return の役割を理解できたと思います。ただし、このウィキペディアの例では、それらのアプリケーションの構文に少し混乱しています。

add :: Maybe Int -> Maybe Int -> Maybe Int
add mx my =             -- Adds two values of type (Maybe Int), where each input value can be Nothing
  mx >>= (\x ->         -- Extracts value x if mx is not Nothing
    my >>= (\y ->       -- Extracts value y if my is not Nothing
      return (x + y)))  -- Wraps value (x+y), returning the sum as a value of type (Maybe Int)

この機能の意図がよくわかりました。評価の順序について少し混乱しています。関数のどの部分が評価されるか行ごとに誰かが表示できますか (mx と my が Maybe Int 型で、mx = Just x' と my = Just y' で、x' と y' が Int 値であると仮定します)?

私はそれがそのようなものだと思います:

mx >>= (\x -> my >>= (\y -> return (x + y)))    --- original
(\x -> my >>= (\y -> return (x + y))) x         --- mx is Just Int, then apply the function in x
(\x -> my >>= (\y -> return (x + y)))           --- x is the first argument. Then I get confused. What's the second part of the function?
4

1 に答える 1