Saber2pr's Blog

BasicConceptsOfFunctionalProgramming

Basic Concepts of Haskell functional programming

  1. Functor
fmap (+1) [1,2,3] -- [2,3,4]

(+1) <
gt; [1,2,3] -- [2,3,4] (+1) <
gt; Just 1 -- Just 2
  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