Saber2pr's Blog

函数式编程基本概念

函数式编程基本概念

  1. Functor
fmap (+1) [1,2,3] -- [2,3,4]
  1. Applicative
Just (+1) <*> Just 1 -- Just 2
  1. Monad
Just 1 >>= \x -> Just $ x + 1 -- Just 2

Just 1 >>= \x -> pure $ x + 1 -- Just 2

Just 1 >>= \x -> return $ x + 1 -- Just 2

do block

module Main where

test :: Monad m => m Integer
test = pure 233

result :: Monad m => m Integer
result = do
  value <- test
  return $ value + 1

main :: IO ()
main = result >>= print