Git Product home page Git Product logo

monad's Introduction

Monad for dummies

If like me you think monad is a pain in the a** to understand, you would like to understand it with code rather than with "for ever" explanations.

  • Explore: monad in different languages.

  • Create: our own monad and see it's like a wrapper for a context.

Quick start

Haskell or using monads without knowing it

  • Go into src/hs and compile with make to generate the binary

Here is the code:

main = do  
    putStrLn "Hello, what's your name?"  
    name <- getLine  
    putStrLn "Your age?"  
    age <- getLine  
    putStrLn ("Hey " ++ name ++ ", you rock at " ++ age ++ "!")
    return 0

You think this Haskell code does the same thing than the following C code?

int main() {
    printf("Hello, what's your name?\n");
    char name[16]; scanf("%s", name);
    printf("Your age?\n");
    int age; scanf("%d", &age);
    printf("Hey %s, you rock at %d!\n", name, age);
    return 0;
}

After knowing what a monad is and how it works, you will be convinced that those two codes have absolutely nothing in common.

What happened in Haskell?

The do notation, assignement <- and return are all implemented with monads. In other words, this is syntactic sugar.

main =
    putStrLn "Hello, what's your name?" >>= \_ ->
    getLine  >>= \ name ->
    putStrLn "Your age?"   >>= \ _ ->
    getLine >>= \ age ->
    putStrLn ("Hey " ++ name ++ ", you rock at " ++ age ++ "!") >>
    return 0

Got it? Welcome to functional programming :)

You have noticed that return has not been desugared. Its type is Integer -> IO Integer here.

The IO monad is the way to communicate with the real world in a pure functional programming language like Haskell.

Back to comparison with C

  • Sequence: is an ordered set of instructions. For instance:
int c = 3 ; printf("%d\n",c) ; return c ;

The same in Haskell is:

return 3 >>= \ c -> putStrLn(show c) >> return c

A sequence of instructions in C is equivalent to an expression in Haskell.

Here, we are touching the basic, and very important, differences between imperative programming and functional programming.

  • Assignement: binds a value to a variable. For instance:
int x = 4

VS

(\x -> ...) 4
  • Return: ends the computation in C. Builds a monad in Haskell.

License

Monad is made under the terms of the MIT license.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.