How does forever works ?

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

How does forever works ?

Olivier Revollat
From what I understand, this code

main = forever $ do
  putStrLn "OK !"

is equivalent to this one :

main = do
  putStrLn "OK !"
  main


In the second case, it's a simple recursion, so far so good ... but when I look at the implementation of "forever" i can't wrap my head around :


forever a   = let a' = a *> a' in a'
How does this works ? How does this make an infinite loop ?

I understand that *> discard his right argument but it doesn't help me understand how forever implement an infinite loop ...





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

Re: How does forever works ?

Francesco Ariis
Hello Olivier,

On Mon, Jul 09, 2018 at 01:31:07PM +0200, Olivier Revollat wrote:
> In the second case, it's a simple recursion, so far so good ... but when I
> look at the implementation of "forever" i can't wrap my head around :
>
> http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Control.Monad.html#forever

    forever a   = let a' = a *> a' in a'

`*>` is the same as `>>` (if you have ever met `>>`), with the difference
that the latter works on monads only. In do notation, we could write

    forever :: Monad m => m a -> m b
    forever a = let a' = do a
                            a' in
                do a'

Which should be easier to get: we `do` a'; a' is nothing but a and a',
so we do a and then a', which is nothing but a and a', etc.

Does this explanation feel right?

As soon as you can, familiarise yourself what the do notation desugars to:
in my opinion plain operators are clearer
-F
_______________________________________________
Beginners mailing list
[hidden email]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
Reply | Threaded
Open this post in threaded view
|

Re: How does forever works ?

Olivier Revollat
Hey I think it's a bit clearer now .... need to meditate on this :) Thanks !

Le lun. 9 juil. 2018 à 13:55, Francesco Ariis <[hidden email]> a écrit :
Hello Olivier,

On Mon, Jul 09, 2018 at 01:31:07PM +0200, Olivier Revollat wrote:
> In the second case, it's a simple recursion, so far so good ... but when I
> look at the implementation of "forever" i can't wrap my head around :
>
> http://hackage.haskell.org/package/base-4.11.1.0/docs/src/Control.Monad.html#forever

    forever a   = let a' = a *> a' in a'

`*>` is the same as `>>` (if you have ever met `>>`), with the difference
that the latter works on monads only. In do notation, we could write

    forever :: Monad m => m a -> m b
    forever a = let a' = do a
                            a' in
                do a'

Which should be easier to get: we `do` a'; a' is nothing but a and a',
so we do a and then a', which is nothing but a and a', etc.

Does this explanation feel right?

As soon as you can, familiarise yourself what the do notation desugars to:
in my opinion plain operators are clearer
-F
_______________________________________________
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