How does this weird monad bind work?

Luís Hendrix

This is probably a very noob question but I was playing around with the bind operator in Haskell and I encountered a way to repeat a string using it.

[1..3] >>= const "Hey"
-- Yields "HeyHeyHey"
[1..3] >>= return "Hey"
-- Also yields the same result

I understand how >>= (\_ -> return "Hey") would yield ["Hey", "Hey", "Hey"] but I don't understand why (\_ -> "Hey") repeats the string or why >>= return "Hey" does the same thing.

leftaroundabout

I understand how >>= (\_ -> return "Hey") would yield ["Hey", "Hey", "Hey"]

right. return "Hey" is in this case the same as ["Hey"], because

instance Monad [] where
  return x = [x]

So

([1..3] >>= \_ -> return "Hey")
  ≡  ([1..3] >>= \_ -> ["Hey"])
  ≡  ["Hey"] ++ ["Hey"] ++ ["Hey"]
  ≡  ["Hey", "Hey", "Hey"]

Now, >>= (\_ -> "Hey") can also be be written with a list-result in the lambda, because strings are just lists of characters.

([1..3] >>= \_ -> "Hey")
  ≡  ([1..3] >>= \_ -> ['H','e','y'])
  ≡  ['H','e','y'] ++ ['H','e','y'] ++ ['H','e','y']
  ≡  ['H','e','y','H','e','y','H','e','y']
  ≡  "HeyHeyHey"

As for >>= return "Hey", that's a different beast. The return belongs to a completely different monad here, namely the function functor.

instance Monad (x->) where
  return y = const y

Hence it's kind of clear that ([1..3] >>= const "Hey") and ([1..3] >>= return "Hey") give the same result: in that example, return is just another name for const!

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

How does IO monad work in System.Random

From Dev

How does the state monad work? (without code explanation)

From Dev

How does the Reader monad's "ask" function work?

From Dev

haskell, monad, definition of bind, and weird pattern matching on an output value?

From Dev

What is Ember.bind and how does it work?

From Dev

How does ZeroMQ connect and bind work internally

From Dev

How and when does the bind function work?

From Dev

How does this weird JavaScript function for primality check work?

From Dev

bind of jquery does not work

From Dev

bind of jquery does not work

From Dev

Why does 'cal' use weird 08 / ^H / \b terminal code for highlighting and how does that work?

From Dev

How to get ReaderT to work with another monad transformer?

From Dev

How to work with mutable structures in the IO monad

From Dev

How to get ReaderT to work with another monad transformer?

From Dev

How does this State monad code works?

From Dev

How does Monad conform to the composition of function

From Dev

How does Mithril component work here is some weird experience I had

From Dev

JavaScript - Weird usage of Date.getDate() for to get the days-count of a month. How does that work?

From Dev

Haskell foldl Monad bind

From Dev

How does shared_from_this in std::bind work where object does not exist?

From Dev

Why ng-bind does not work with array?

From Dev

JavaScript bind method does not work on getter property

From Dev

Why does bind not work with pass by reference?

From Dev

ListBox Grouping - Bind Header with type does not work

From Dev

Does canvas.bind work in a while loop

From Dev

AngularJS $watch does not work with bind object

From Dev

Bind Service with Altbeacon does not work after second bind

From Dev

In a Warren's Abstract Machine, how does bind work, if one of the arguments is a register?

From Dev

Monad bind insists on different type?

Related Related

  1. 1

    How does IO monad work in System.Random

  2. 2

    How does the state monad work? (without code explanation)

  3. 3

    How does the Reader monad's "ask" function work?

  4. 4

    haskell, monad, definition of bind, and weird pattern matching on an output value?

  5. 5

    What is Ember.bind and how does it work?

  6. 6

    How does ZeroMQ connect and bind work internally

  7. 7

    How and when does the bind function work?

  8. 8

    How does this weird JavaScript function for primality check work?

  9. 9

    bind of jquery does not work

  10. 10

    bind of jquery does not work

  11. 11

    Why does 'cal' use weird 08 / ^H / \b terminal code for highlighting and how does that work?

  12. 12

    How to get ReaderT to work with another monad transformer?

  13. 13

    How to work with mutable structures in the IO monad

  14. 14

    How to get ReaderT to work with another monad transformer?

  15. 15

    How does this State monad code works?

  16. 16

    How does Monad conform to the composition of function

  17. 17

    How does Mithril component work here is some weird experience I had

  18. 18

    JavaScript - Weird usage of Date.getDate() for to get the days-count of a month. How does that work?

  19. 19

    Haskell foldl Monad bind

  20. 20

    How does shared_from_this in std::bind work where object does not exist?

  21. 21

    Why ng-bind does not work with array?

  22. 22

    JavaScript bind method does not work on getter property

  23. 23

    Why does bind not work with pass by reference?

  24. 24

    ListBox Grouping - Bind Header with type does not work

  25. 25

    Does canvas.bind work in a while loop

  26. 26

    AngularJS $watch does not work with bind object

  27. 27

    Bind Service with Altbeacon does not work after second bind

  28. 28

    In a Warren's Abstract Machine, how does bind work, if one of the arguments is a register?

  29. 29

    Monad bind insists on different type?

HotTag

Archive