Send Beginners mailing list submissions to
[email protected]
To subscribe or unsubscribe via the World Wide Web, visit
http://mail.haskell.org/cgi-bin/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: Is it possible to check list length at compile time?
(Marcin Mrotek)
2. Re: Have I just broken Haskell? (Anurag Ohri)
3. Re: Have I just broken Haskell? (Chadda? Fouch?)
----------------------------------------------------------------------
Message: 1
Date: Wed, 18 Mar 2015 00:00:48 +0100
From: Marcin Mrotek <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Is it possible to check list length
at compile time?
Message-ID:
<CAJcfPz=y+4yu1kig7zhcp-034w12w0c5kewdvjfjt3aghrt...@mail.gmail.com>
Content-Type: text/plain; charset=UTF-8
I think you're looking for dependend types or refinement types. It's
sort of possible in Haskell, but look into Agda, Idris, F#, and this
thread:
http://www.reddit.com/r/haskell/comments/2z5l9y/question_type_system_and_ndimensional_vectors/
Best regards,
Marcin Mrotek
------------------------------
Message: 2
Date: Wed, 18 Mar 2015 07:21:26 +0530
From: Anurag Ohri <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Have I just broken Haskell?
Message-ID:
<CAONtWhy=w-rhn6ycw7o22ltby9uwdommhhwzjzer5kbvigp...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
I recently encountered the same Exception. It occurs when you open a file
but don't close it using "hClose".
On Mar 18, 2015 3:46 AM, "Bob Ippolito" <[email protected]> wrote:
> On Tue, Mar 17, 2015 at 2:55 PM, Mark Carter <[email protected]>
> wrote:
>
>> Persuant to my previous question, I have made some progress, but there is
>> something odd going on.
>>
>> Suppose we want to implement the following algorithm, which is a little
>> simpler than the original problem:
>>
>> The user types the name of a file as input. Haskell then puts the text
>> "it works" to that file. I tried this:
>>
>> import System.IO
>> let openFileWrite name = openFile name WriteMode
>> h :: IO Handle
>> h <- fmap openFileWrite getLine -- you need to type in a file name
>>
>> This looks wrong, because h presumably needs to be a fixed Handle. Being
>> an IO Handle seems to imply it is mutable. Haskell doesn't complain, though.
>>
>
> This is wrong because the type of `h` should be Handle, not IO Handle.
> `fmap` is not the correct function to use here, you use `fmap` to lift a
> pure function into some Functor. What you need to do is sequence these two
> IO actions, like this:
>
> name <- getLine
> h <- openFileWrite name
>
> Which is equivalent to this:
>
> h <- getLine >>= openFileWrite
>
> or this (`=<<` is just a flipped `>>=` operator):
>
> h <- openFileWrite =<< getLine
>
> -bob
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150318/802358a3/attachment-0001.html>
------------------------------
Message: 3
Date: Wed, 18 Mar 2015 07:20:39 +0100
From: Chadda? Fouch? <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Have I just broken Haskell?
Message-ID:
<canfjzrz4nrff0waqnbv9c99lrfynpcy35hzq8andxdcpepk...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Tue, Mar 17, 2015 at 10:55 PM, Mark Carter <[email protected]> wrote:
> Persuant to my previous question, I have made some progress, but there is
> something odd going on.
>
> Suppose we want to implement the following algorithm, which is a little
> simpler than the original problem:
>
> The user types the name of a file as input. Haskell then puts the text "it
> works" to that file. I tried this:
>
> import System.IO
> let openFileWrite name = openFile name WriteMode
> h :: IO Handle
> h <- fmap openFileWrite getLine -- you need to type in a file name
>
> This looks wrong, because h presumably needs to be a fixed Handle. Being
> an IO Handle seems to imply it is mutable. Haskell doesn't complain, though.
>
No it doesn't, nothing is "mutable" in Haskell, you can have IORef or STRef
whose content can be mutated in the IO or ST monad but the IORef or STRef
themselves won't be mutable. IO Handle is not "a mutable Handle", it's an
IO action that returns an Handle... And that's exactly the type of h in
your code !
That's because this code doesn't open anything, it read a line and thanks
to fmap, it applies openFileWrite to this line, giving you an "IO Handle"
in h, an action that would give you an Handle if it was executed... but it
is not executed, it is just stocked in h.
The type of "fmap openFileWrite getLine" is IO (IO Handle), not as you
probably wanted IO Handle.
As Bob said, if you want an Handle, you shouldn't use fmap but rather
(>>=). Or you could do :
act :: IO handle <- fmap openFileWrite getLine
h :: Handle <- act
but that's really not the usual way to do it (on the other hand it's
interesting because it shows you that "IO a" are just values, you can
manipulate them just like normal values.
Basically your code was "correct", it just didn't give you an Handle (that
would have been clear as soon as you tried to use h as an Handle) !
--
Jeda?
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150318/955826ad/attachment-0001.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 81, Issue 49
*****************************************