So I'm trying to understand exactly how the Haskell do
notation works.
I understand that it is used together with monads and that it basically expands (since it's actually syntactic sugar) into anonymous functions connected with either bind (>>=
) or then (>>
) as shown here https://en.wikibooks.org/wiki/Haskell/Syntactic_sugar#Do_notation.
However my question is why the following command
Prelude> do [1, 2, 3]; "hello"
returns
"hellohellohello"
I know that arrays are actually monads (and that strings are arrays of chars) but I fail to see how this results in the behavior above.
do [1, 2, 3]; "hello"
desugars to
[1, 2, 3] >> "hello"
which is the same as
[1, 2, 3] >>= (\_ -> "hello")
which is the same as
concatMap (\_ -> "hello") [1, 2, 3]
which is the same as
concat (map (\_ -> "hello") [1, 2, 3])
which is the same as
concat [(\_ -> "hello") 1, (\_ -> "hello") 2, (\_ -> "hello") 3])
which is the same as
concat ["hello","hello","hello"]
which is the same as
"hellohellohello"