Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://www.haskell.org/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1. Re:  help with IO guards (Julian Birch)
   2. Re:  help with IO guards (Christopher Allen)
   3. Re:  help with IO guards (Miro Karpis)


----------------------------------------------------------------------

Message: 1
Date: Thu, 15 Jan 2015 21:27:19 +0000
From: Julian Birch <[email protected]>
To: "[email protected]" <[email protected]>,  The
        Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] help with IO guards
Message-ID:
        <CAB0TuzC4tJk=6kkxf3zvbjwjjsfzfwb1eucs0yrlr+jdr97...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

Not really, for the same reason: a guard needs a Bool, and you can't get a
Bool from IO Bool.

On Thursday, January 15, 2015, Miro Karpis <[email protected]>
wrote:

> many thanks,....but then I unfortunately don't understand how can I fix my
> initial problem:
>
> to use IO check in guards - is that possible?
>
> Regards,
> Miro
>
> On Thu, Jan 15, 2015 at 10:12 PM, Julian Birch <[email protected]
> <javascript:_e(%7B%7D,'cvml','[email protected]');>> wrote:
>
>> Going back to an earlier question: a monad is a bit like a roach motel.
>> You can check in but you can't leave. (This isn't true of every Monad, but
>> the point is there's no guarantees.) In particular, you can't go from IO
>> String to String _at all_. But you can, through Functor, pass it to a
>> function that takes a plain String. And through Monad, you can turn IO
>> (IO String) back to IO String.
>>
>> Hope this helps.
>>
>>
>> On Thursday, January 15, 2015, Marcin Mrotek <[email protected]
>> <javascript:_e(%7B%7D,'cvml','[email protected]');>> wrote:
>>
>>> Hello,
>>>
>>> A list ([]) is also a monad, and a String is defined as a list of
>>> characters ([Char]). So in your example, it's as if you were trying to
>>> use (>>=) operator on two different monads ([] and IO), which is
>>> impossible. To make a pure value a monadic value, you need to use
>>> return:
>>>
>>> g = readLn >>= (\a -> return (f a))
>>>
>>> which is equivalent to composing f with return:
>>>
>>> g = readLn >>= return.f
>>>
>>> Regards,
>>> Marcin
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>
>>
>> --
>> Sent from an iPhone, please excuse brevity and typos.
>>
>
>

-- 
Sent from an iPhone, please excuse brevity and typos.
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20150115/b656f21c/attachment-0001.html>

------------------------------

Message: 2
Date: Thu, 15 Jan 2015 15:28:55 -0600
From: Christopher Allen <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] help with IO guards
Message-ID:
        <CADnndOo72qe9pBS5kNUwovwqescforLwENmfqq=2=nmqyiw...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

I haven't seen anybody explain the real reasons you can't yank `a` out of
something of type `IO a`, so I thought I'd attempt to clear a few things up.

It's true that you cannot *in general* extract a value of type `a` from
something of type `Monad m => m a` but those reasons are *not* why you
can't in the case of IO. You can't with Monad in general because the
typeclass doesn't provide a method with the type `m a -> a`, in fact, it
only provides the opposite: `a -> m a`.

The real reason you can't pattern match on or otherwise extract values from
IO is that it's an abstract datatype - on purpose. The constructor is not
exported, the internals are hidden.

You are using a non-strict programming language. The IO datatype is how the
implementation knows not to replace the thunk with its value when evaluated.

Want to see what I mean?

Play with some code that uses
https://hackage.haskell.org/package/time-1.3/docs/Data-Time-Clock.html#v:getCurrentTime
to get the current time. Note how each time you force evaluation of the `IO
UTCTime` you're getting a new UTCTime each time. Then wrap it in
`unsafePerformIO` - it'll only get executed once. This is *definitely* not
the semantics you want, you're too new to know when you'd want the unsafe*
functions for now.

If you aren't comfortable with monads, no amount of thrashing is going to
let you avoid using Functor/Applicative/Monad if you want to interact with
values produced in IO.

I wrote a guide for learning Haskell, it covers Functor/Applicative/Monad
which are *everywhere* - not just for use with IO. This is the guide:
https://github.com/bitemyapp/learnhaskell

There are people used to teaching and assisting with the courses
recommended in my guide (cis194 & NICTA course) on Freenode IRC at
#haskell-beginners. There are tons of people equipped to help you in
#haskell as well.

--- Chris Allen


On Thu, Jan 15, 2015 at 3:12 PM, Julian Birch <[email protected]>
wrote:

> Going back to an earlier question: a monad is a bit like a roach motel.
> You can check in but you can't leave. (This isn't true of every Monad, but
> the point is there's no guarantees.) In particular, you can't go from IO
> String to String _at all_. But you can, through Functor, pass it to a
> function that takes a plain String. And through Monad, you can turn IO
> (IO String) back to IO String.
>
> Hope this helps.
>
>
> On Thursday, January 15, 2015, Marcin Mrotek <[email protected]>
> wrote:
>
>> Hello,
>>
>> A list ([]) is also a monad, and a String is defined as a list of
>> characters ([Char]). So in your example, it's as if you were trying to
>> use (>>=) operator on two different monads ([] and IO), which is
>> impossible. To make a pure value a monadic value, you need to use
>> return:
>>
>> g = readLn >>= (\a -> return (f a))
>>
>> which is equivalent to composing f with return:
>>
>> g = readLn >>= return.f
>>
>> Regards,
>> Marcin
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>
>
> --
> Sent from an iPhone, please excuse brevity and typos.
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20150115/75061d30/attachment-0001.html>

------------------------------

Message: 3
Date: Thu, 15 Jan 2015 22:34:49 +0100
From: Miro Karpis <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] help with IO guards
Message-ID:
        <CAJnnbxHuoZO75krdARSknknHVbbStz=b1v44nt_clpetvu2...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

many thanks! I will take a look at it.

cheers and thanks to all :-)

On Thu, Jan 15, 2015 at 10:28 PM, Christopher Allen <[email protected]>
wrote:

> I haven't seen anybody explain the real reasons you can't yank `a` out of
> something of type `IO a`, so I thought I'd attempt to clear a few things up.
>
> It's true that you cannot *in general* extract a value of type `a` from
> something of type `Monad m => m a` but those reasons are *not* why you
> can't in the case of IO. You can't with Monad in general because the
> typeclass doesn't provide a method with the type `m a -> a`, in fact, it
> only provides the opposite: `a -> m a`.
>
> The real reason you can't pattern match on or otherwise extract values
> from IO is that it's an abstract datatype - on purpose. The constructor is
> not exported, the internals are hidden.
>
> You are using a non-strict programming language. The IO datatype is how
> the implementation knows not to replace the thunk with its value when
> evaluated.
>
> Want to see what I mean?
>
> Play with some code that uses
> https://hackage.haskell.org/package/time-1.3/docs/Data-Time-Clock.html#v:getCurrentTime
> to get the current time. Note how each time you force evaluation of the `IO
> UTCTime` you're getting a new UTCTime each time. Then wrap it in
> `unsafePerformIO` - it'll only get executed once. This is *definitely* not
> the semantics you want, you're too new to know when you'd want the unsafe*
> functions for now.
>
> If you aren't comfortable with monads, no amount of thrashing is going to
> let you avoid using Functor/Applicative/Monad if you want to interact with
> values produced in IO.
>
> I wrote a guide for learning Haskell, it covers Functor/Applicative/Monad
> which are *everywhere* - not just for use with IO. This is the guide:
> https://github.com/bitemyapp/learnhaskell
>
> There are people used to teaching and assisting with the courses
> recommended in my guide (cis194 & NICTA course) on Freenode IRC at
> #haskell-beginners. There are tons of people equipped to help you in
> #haskell as well.
>
> --- Chris Allen
>
>
> On Thu, Jan 15, 2015 at 3:12 PM, Julian Birch <[email protected]>
> wrote:
>
>> Going back to an earlier question: a monad is a bit like a roach motel.
>> You can check in but you can't leave. (This isn't true of every Monad, but
>> the point is there's no guarantees.) In particular, you can't go from IO
>> String to String _at all_. But you can, through Functor, pass it to a
>> function that takes a plain String. And through Monad, you can turn IO
>> (IO String) back to IO String.
>>
>> Hope this helps.
>>
>>
>> On Thursday, January 15, 2015, Marcin Mrotek <[email protected]>
>> wrote:
>>
>>> Hello,
>>>
>>> A list ([]) is also a monad, and a String is defined as a list of
>>> characters ([Char]). So in your example, it's as if you were trying to
>>> use (>>=) operator on two different monads ([] and IO), which is
>>> impossible. To make a pure value a monadic value, you need to use
>>> return:
>>>
>>> g = readLn >>= (\a -> return (f a))
>>>
>>> which is equivalent to composing f with return:
>>>
>>> g = readLn >>= return.f
>>>
>>> Regards,
>>> Marcin
>>> _______________________________________________
>>> Beginners mailing list
>>> [email protected]
>>> http://www.haskell.org/mailman/listinfo/beginners
>>>
>>
>>
>> --
>> Sent from an iPhone, please excuse brevity and typos.
>>
>> _______________________________________________
>> Beginners mailing list
>> [email protected]
>> http://www.haskell.org/mailman/listinfo/beginners
>>
>>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: 
<http://www.haskell.org/pipermail/beginners/attachments/20150115/516c876e/attachment.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 79, Issue 19
*****************************************

Reply via email to