Do IO actions *have* to be glued by the do syntax?

Previous Topic Next Topic
 
classic Classic list List threaded Threaded
14 messages Options
Reply | Threaded
Open this post in threaded view
|

Do IO actions *have* to be glued by the do syntax?

Olumide
Hello List,

Do IO actions *have* to be glued by the do syntax? Many, if not all, the
examples that I've come across in LYH seem to suggest so. And if so, why?

BTW, if possible I'd appreciate an explanation that does not resort to
monads. I haven't studied them yet and I'm sure I'd struggle to
understand any explanation that resorts to monads.

Thanks,

- Olumide
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Do IO actions *have* to be glued by the do syntax?

Quanyang Liu
On Tue, Mar 22 2016 at 18:52:41 +0800, Olumide wrote:

> Hello List,
>
> Do IO actions *have* to be glued by the do syntax? Many, if not all,
> the examples that I've come across in LYH seem to suggest so. And if
> so, why?
>
> BTW, if possible I'd appreciate an explanation that does not resort to
> monads. I haven't studied them yet and I'm sure I'd struggle to
> understand any explanation that resorts to monads.
>

I think you should google that first... do blocks are just sugar...

> Thanks,
>
> - Olumide

--
Thanks,
Quanyang
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Do IO actions *have* to be glued by the do syntax?

Daniel P. Wright
In reply to this post by Olumide
Hello Olumide,

The simple answer is "no", do syntax is an entirely optional piece of syntactic sugar that is just there to make the code easier to read in some cases.  But, IO actions do have to be glued together by something, and that something is the >>= operator (pronounced "bind").

>>= takes an IO action on the left, (let's use getLine, which reads a line from the user, for our example) and a function on the right which takes the result of that action and returns the next IO action to perform.  For example:

> getLine >>= (\line -> putStrLn line)

Here we can see the getLine action on the left.  The result of that is passed, through the mechanism provided by >>=, to the function on the right, which is a lambda function taking that line as a parameter and calling the action "putStrLn" to print it back out again.  This is equivalent to the following do notation:

> do
>   line <- getLine
>   putStrLn line

What if you want to string together more than two actions, though?  For example, say we wanted to get two lines and concatenation them in the output, like so:

> do
>   line1 <- getLine
>   line2 <- getLine
>   putStrLn (line1 ++ line2)

Since >>= can only take two parameters, what do we do?  Well, since the result of >>= is itself an IO action, we can use it again in the function on the right!  Like this:

> getLine >>= (\line1 -> (getLine >>= (\line2 -> putStrLn (line1 ++ line2))))

Now, this may be starting to look a little bit like lisp with all the parentheses!  In fact, because of the associativity of the >>= operator, the brackets are all optional, but they may be helpful for you to see what's going on.  Without the brackets, it looks like this:

> getLine >>= \line1 -> getLine >>= \line2 -> putStrLn (line1 ++ line2)

Which, if you were to lay it out a little differently, looks like this:

> getLine >>= \line1 ->
> getLine >>= \line2 ->
> putStrLn (line1 ++ line2)

...Starting to look a little more like the do syntax version above, isn't it?

What if you don't care about the value of the parameter?  Well, you could ignore it using the _ syntax like this:

> getLine >>= \_ -> putStrLn "42"

But Haskell actually provides an operator to save you the effort, called >>, so you can do:

> getLine >> putStrLn "42"

Do notation is a purely syntactic transformation that emits code using these operators.

Incidentally, the function on the right doesn't have to be a lambda function!  Any function taking a value of whatever type is returned by the action on the left will do, and in the case of getLine/putStrLn they match!  So the first example could be rewritten:

> getLine >>= putStrLn

Which many would argue looks nicer than the equivalent do notation.

Hope that helps, sorry if it was a little verbose!

-Dani.

PS- I know you said not to mention monads, but if you followed everything above you're 90% of the way there anyway -- a monad is just a type class providing the >>= operator, and the "return" function, which takes a value and turns it into an action returning that value (at least in terms of the IO monad).

> On Mar 22, 2016, at 19:52, Olumide <[hidden email]> wrote:
>
> Hello List,
>
> Do IO actions *have* to be glued by the do syntax? Many, if not all, the examples that I've come across in LYH seem to suggest so. And if so, why?
>
> BTW, if possible I'd appreciate an explanation that does not resort to monads. I haven't studied them yet and I'm sure I'd struggle to understand any explanation that resorts to monads.
>
> Thanks,
>
> - Olumide
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Do IO actions *have* to be glued by the do syntax?

Olumide
In reply to this post by Quanyang Liu
Quanyang,

do bocks are *not* sugar, and please refer to Daniel's answer for an
example of how to answer a beginner's question.

- Olumide

On 22/03/2016 11:06, Quanyang Liu wrote:

> On Tue, Mar 22 2016 at 18:52:41 +0800, Olumide wrote:
>> Hello List,
>>
>> Do IO actions *have* to be glued by the do syntax? Many, if not all,
>> the examples that I've come across in LYH seem to suggest so. And if
>> so, why?
>>
>> BTW, if possible I'd appreciate an explanation that does not resort to
>> monads. I haven't studied them yet and I'm sure I'd struggle to
>> understand any explanation that resorts to monads.
>>
>
> I think you should google that first... do blocks are just sugar...
>
>> Thanks,
>>
>> - Olumide
>

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Do IO actions *have* to be glued by the do syntax?

Martin Vlk
Hi Olumide,

Yes, do blocks are syntactic sugar.
Yes you are right in that answers here should be .. a bit friendlier.
But also in my opinion your last post is a bit.. arrogant sounding...

M.

Dne Út, 22. březen 2016 v 12:36 h uživatel Olumide napsal:

> Quanyang,
>
> do bocks are *not* sugar, and please refer to Daniel's answer for an
> example of how to answer a beginner's question.
>
> - Olumide
>
> On 22/03/2016 11:06, Quanyang Liu wrote:
> > On Tue, Mar 22 2016 at 18:52:41 +0800, Olumide wrote:
> >> Hello List,
> >>
> >> Do IO actions *have* to be glued by the do syntax? Many, if not all,
> >> the examples that I've come across in LYH seem to suggest so. And if
> >> so, why?
> >>
> >> BTW, if possible I'd appreciate an explanation that does not resort to
> >> monads. I haven't studied them yet and I'm sure I'd struggle to
> >> understand any explanation that resorts to monads.
> >>
> >
> > I think you should google that first... do blocks are just sugar...
> >
> >> Thanks,
> >>
> >> - Olumide
> >
>
> _______________________________________________
> Beginners mailing list
> [hidden email]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Do IO actions *have* to be glued by the do syntax?

Olumide
In reply to this post by Olumide
Can I also add that this sort of elitist, unhelpful quip of a
pseudo-answer is the reason that I've largely avoided asking Haskell
beginner questions on stackoverflow.

Haskell is a wonderful language with a steep learning curve and I
believe that it is the intention of this group grow the user base of the
language by create a place where beginners will not be afraid to ask
questions. And hasty ill-thought, poorly worded answers like Quanyang's
won't help the language get there sooner.

- Olumide


On 22/03/2016 11:36, Olumide wrote:

> Quanyang,
>
> do bocks are *not* sugar, and please refer to Daniel's answer for an
> example of how to answer a beginner's question.
>
> - Olumide
>
> On 22/03/2016 11:06, Quanyang Liu wrote:
>> On Tue, Mar 22 2016 at 18:52:41 +0800, Olumide wrote:
>>> Hello List,
>>>
>>> Do IO actions *have* to be glued by the do syntax? Many, if not all,
>>> the examples that I've come across in LYH seem to suggest so. And if
>>> so, why?
>>>
>>> BTW, if possible I'd appreciate an explanation that does not resort to
>>> monads. I haven't studied them yet and I'm sure I'd struggle to
>>> understand any explanation that resorts to monads.
>>>
>>
>> I think you should google that first... do blocks are just sugar...
>>
>>> Thanks,
>>>
>>> - Olumide
>>
>

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Do IO actions *have* to be glued by the do syntax?

Sumit Sahrawat, Maths & Computing,
 IIT (BHU)
In reply to this post by Olumide

Olumide, do blocks are indeed syntactic sugar. They are a piece of syntax that converts to a specific type of expressions, namely those using the bind (>>=) operator.

It is really helpful, when learning to be able to come up with answers in your own. Learning Haskell has always involved combining many different perspectives on the same topic, told differently in different places. It would only help you if you took the time and dug further for answers. It's really satisfying too!

Also, if you haven't, search for the Leavenworth guide. It takes this very question in one of the miscellaneous discussions.

Be nice, be respectful. Have fun learning Haskell :)

On 22-Mar-2016 5:07 pm, "Olumide" <[hidden email]> wrote:
Quanyang,

do bocks are *not* sugar, and please refer to Daniel's answer for an example of how to answer a beginner's question.

- Olumide

On 22/03/2016 11:06, Quanyang Liu wrote:
On Tue, Mar 22 2016 at 18:52:41 +0800, Olumide wrote:
Hello List,

Do IO actions *have* to be glued by the do syntax? Many, if not all,
the examples that I've come across in LYH seem to suggest so. And if
so, why?

BTW, if possible I'd appreciate an explanation that does not resort to
monads. I haven't studied them yet and I'm sure I'd struggle to
understand any explanation that resorts to monads.


I think you should google that first... do blocks are just sugar...

Thanks,

- Olumide


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Do IO actions *have* to be glued by the do syntax?

Sumit Sahrawat, Maths & Computing,
 IIT (BHU)

Sorry, autocorrect changed learnhaskell to something else.

On 22-Mar-2016 5:16 pm, "Sumit Sahrawat, Maths & Computing, IIT (BHU)" <[hidden email]> wrote:

Olumide, do blocks are indeed syntactic sugar. They are a piece of syntax that converts to a specific type of expressions, namely those using the bind (>>=) operator.

It is really helpful, when learning to be able to come up with answers in your own. Learning Haskell has always involved combining many different perspectives on the same topic, told differently in different places. It would only help you if you took the time and dug further for answers. It's really satisfying too!

Also, if you haven't, search for the Leavenworth guide. It takes this very question in one of the miscellaneous discussions.

Be nice, be respectful. Have fun learning Haskell :)

On 22-Mar-2016 5:07 pm, "Olumide" <[hidden email]> wrote:
Quanyang,

do bocks are *not* sugar, and please refer to Daniel's answer for an example of how to answer a beginner's question.

- Olumide

On 22/03/2016 11:06, Quanyang Liu wrote:
On Tue, Mar 22 2016 at 18:52:41 +0800, Olumide wrote:
Hello List,

Do IO actions *have* to be glued by the do syntax? Many, if not all,
the examples that I've come across in LYH seem to suggest so. And if
so, why?

BTW, if possible I'd appreciate an explanation that does not resort to
monads. I haven't studied them yet and I'm sure I'd struggle to
understand any explanation that resorts to monads.


I think you should google that first... do blocks are just sugar...

Thanks,

- Olumide


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Do IO actions *have* to be glued by the do syntax?

Olumide
In reply to this post by Sumit Sahrawat, Maths & Computing, IIT (BHU)
Sumit,

I do, and did, look up other resources but none emphasized that IO
actions have to be bound, and that's why I asked here. I'm sure it will
become clearer when I reach the chapter on monads.

@Martin: I realize that I am equally responsible for the culture of the
list and will also try to be friendlier ...

- Olumide


On 22/03/2016 11:46, Sumit Sahrawat, Maths & Computing, IIT (BHU) wrote:

> Olumide, do blocks are indeed syntactic sugar. They are a piece of
> syntax that converts to a specific type of expressions, namely those
> using the bind (>>=) operator.
>
> It is really helpful, when learning to be able to come up with answers
> in your own. Learning Haskell has always involved combining many
> different perspectives on the same topic, told differently in different
> places. It would only help you if you took the time and dug further for
> answers. It's really satisfying too!
>
> Also, if you haven't, search for the Leavenworth guide. It takes this
> very question in one of the miscellaneous discussions.
>
> Be nice, be respectful. Have fun learning Haskell :)
>
> On 22-Mar-2016 5:07 pm, "Olumide" <[hidden email] <mailto:[hidden email]>>
> wrote:
>
>     Quanyang,
>
>     do bocks are *not* sugar, and please refer to Daniel's answer for an
>     example of how to answer a beginner's question.
>
>     - Olumide
>
>     On 22/03/2016 11:06, Quanyang Liu wrote:
>
>         On Tue, Mar 22 2016 at 18:52:41 +0800, Olumide wrote:
>
>             Hello List,
>
>             Do IO actions *have* to be glued by the do syntax? Many, if
>             not all,
>             the examples that I've come across in LYH seem to suggest
>             so. And if
>             so, why?
>
>             BTW, if possible I'd appreciate an explanation that does not
>             resort to
>             monads. I haven't studied them yet and I'm sure I'd struggle to
>             understand any explanation that resorts to monads.
>
>
>         I think you should google that first... do blocks are just sugar...
>
>             Thanks,
>
>             - Olumide
>
>
>
>     _______________________________________________
>     Beginners mailing list
>     [hidden email] <mailto:[hidden email]>
>     http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
>

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Do IO actions *have* to be glued by the do syntax?

Quanyang Liu
In reply to this post by Olumide
Hi Olumide,

I didn't mean quip... If that offended you, I apologize.

But if you learn to find the answer out yourself, it will be more
efficient, since you don't need to wait for others...

For your question, a single query with 'haskell do' as keyword should be
enough.

On Tue, Mar 22 2016 at 19:46:00 +0800, Olumide wrote:

> Can I also add that this sort of elitist, unhelpful quip of a
> pseudo-answer is the reason that I've largely avoided asking Haskell
> beginner questions on stackoverflow.
>
> Haskell is a wonderful language with a steep learning curve and I
> believe that it is the intention of this group grow the user base of
> the language by create a place where beginners will not be afraid to
> ask questions. And hasty ill-thought, poorly worded answers like
> Quanyang's won't help the language get there sooner.
>
> - Olumide

--
Thanks,
Quanyang
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Do IO actions *have* to be glued by the do syntax?

Olumide
Quanyang,

Thanks for your apology. I apologize too.

I do try to find out answers by myself but was unsuccessful in this case
probably because the answer lay in a topic/chapter of the book (Monads)
that I am yet to read. (I am reading LYH chapter-by-chapter.)

Sometimes our best attempts to find an answer will _fail_ ... and that's
why this mailing list exists. We should give each other the benefit of
the doubt and assume that someone that's asked a question has made some
sort of effort to understand before asking.

Regards,

- Olumide


On 22/03/2016 12:06, Quanyang Liu wrote:

> Hi Olumide,
>
> I didn't mean quip... If that offended you, I apologize.
>
> But if you learn to find the answer out yourself, it will be more
> efficient, since you don't need to wait for others...
>
> For your question, a single query with 'haskell do' as keyword should be
> enough.
>
> On Tue, Mar 22 2016 at 19:46:00 +0800, Olumide wrote:
>> Can I also add that this sort of elitist, unhelpful quip of a
>> pseudo-answer is the reason that I've largely avoided asking Haskell
>> beginner questions on stackoverflow.
>>
>> Haskell is a wonderful language with a steep learning curve and I
>> believe that it is the intention of this group grow the user base of
>> the language by create a place where beginners will not be afraid to
>> ask questions. And hasty ill-thought, poorly worded answers like
>> Quanyang's won't help the language get there sooner.
>>
>> - Olumide
>

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Do IO actions *have* to be glued by the do syntax?

Andrew Bernard
In reply to this post by Sumit Sahrawat, Maths & Computing, IIT (BHU)
Hello Sumit,

What is the Leavenworth guide? Googling for ‘leavenworth guide haskell’ provides no hint at all. Well, it does give a lot of travel guides!

Andrew


On 22/03/2016, 22:46, "Beginners on behalf of Sumit Sahrawat, Maths & Computing, IIT (BHU)" <[hidden email] on behalf of [hidden email]> wrote:

Also, if you haven't, search for the Leavenworth guide. It takes this very question in one of the miscellaneous discussions.

_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Do IO actions *have* to be glued by the do syntax?

Sumit Sahrawat, Maths & Computing,
 IIT (BHU)

Is talking about the learnhaskell guide. The autocorrect showed its magic there :)

It's a huge brr Chris Allen, who's currently writing a huge book about Haskell.

Regards,
  Sumit


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: Do IO actions *have* to be glued by the do syntax?

Sumit Sahrawat, Maths & Computing,
 IIT (BHU)

Again, I meant 'guide by'.

On 22-Mar-2016 7:32 pm, "Sumit Sahrawat, Maths & Computing, IIT (BHU)" <[hidden email]> wrote:

Is talking about the learnhaskell guide. The autocorrect showed its magic there :)

It's a huge brr Chris Allen, who's currently writing a huge book about Haskell.

Regards,
  Sumit


_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners