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: A first try (Manfred Lotz)
2. Re: A first try (Antoine Latter)
3. Re: [web-devel] yesod database select all rows? (Michael Snoyman)
4. Re: A first try (Heinrich Apfelmus)
5. Doing IO in terms of Arrow (Haisheng Wu)
6. book question (Roelof Wobben)
7. Re: A first try (Manfred Lotz)
8. Re: book question (Raphael P?bst)
----------------------------------------------------------------------
Message: 1
Date: Mon, 27 Jun 2011 04:57:06 +0200
From: Manfred Lotz <[email protected]>
Subject: Re: [Haskell-beginners] A first try
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=US-ASCII
On Sun, 26 Jun 2011 17:32:02 -0400
David Place <[email protected]> wrote:
>
>
> On Jun 26, 2011, at 4:59 PM, Manfred Lotz wrote:
>
> > Is there any problem in the code snippet I pasted? If so I would
> > like to get a hint of course.
>
> There really isn't enough code in your snippet to be able to say.
> The rule is that if you return a lazy data structure from withFile
> you may find the file has been closed before you have read the data.
> If your parseXMLDoc is strict then you will get away with it. I
> feel that isn't very nice.
>
What I had learned (and it seems to work fine) is to force the result
which gets done here in return $!
insertXml (stat, m) xf =
U.withBinaryFile xf ReadMode
(\handle -> do ct <- getXmlContent xf handle
... some code...
return $! (stat',m'))
So I hope that is the one and important recipe to add when using
withBinaryFile.
--
Manfred
------------------------------
Message: 2
Date: Mon, 27 Jun 2011 12:39:42 +0900
From: Antoine Latter <[email protected]>
Subject: Re: [Haskell-beginners] A first try
To: Manfred Lotz <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
On Mon, Jun 27, 2011 at 11:57 AM, Manfred Lotz <[email protected]> wrote:
> On Sun, 26 Jun 2011 17:32:02 -0400
> David Place <[email protected]> wrote:
>
>>
>>
>> On Jun 26, 2011, at 4:59 PM, Manfred Lotz wrote:
>>
>> > Is there any problem in the code snippet I pasted? If so I would
>> > like to get a hint of course.
>>
>> There really isn't enough code in your snippet to be able to say.
>> The rule is that if you return a lazy data structure from withFile
>> you may find the file has been closed before you have read the data.
>> If your parseXMLDoc is strict then you will get away with it. ? I
>> feel that isn't very nice.
>>
>
> What I had learned (and it seems to work fine) is to force the result
> which gets done here in return $!
>
> insertXml (stat, m) xf =
> ?U.withBinaryFile xf ReadMode
> ? ? (\handle -> do ct <- getXmlContent xf handle
> ? ? ? ? ?... some code...
> ? ? ? ? return $! (stat',m'))
>
> So I hope that is the one and important recipe to add when using
> withBinaryFile.
>
The operator ($!) isn't recursive, so all that it does is force the
tuple you're returning, not the contents of the tuple.
Internally it calls 'seq', which only forces its argument enough to
determine which constructor it was called with. Sometimes this can be
enough to do what you want, but not usually. And certainly not in this
case.
Antoine
>
>
> --
> Manfred
>
>
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
------------------------------
Message: 3
Date: Mon, 27 Jun 2011 06:52:47 +0300
From: Michael Snoyman <[email protected]>
Subject: Re: [Haskell-beginners] [web-devel] yesod database select all
rows?
To: Eric Schug <[email protected]>
Cc: [email protected], web-devel <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
On Mon, Jun 27, 2011 at 2:20 AM, Eric Schug <[email protected]> wrote:
> Hello,
> It does not seem obvious to me how to perform the equivalent of
>
> SELECT * FROM table_name
> SELECT field1,field2 FROM table_name
>
> examples show something of the form
> rows <- runDB $ selectList [ Field1Eq value1, Field2Gt value2] [ ] 0 0
> But I don't see how the name of the table is determined nor how to select
> additional fields to extract.
>
> What would this do?
> rows <- runDB $ selectList [ ] [ ] 0 0
This all has to do with Haskell type trickery. If GHC can determine
the expected type of rows based on how you use it, it will work. If it
can't, it will complain about ambiguous types. If you want to select
from the Person entity, you can do something like:
rows <- runDB $ selectList ([] :: [Filter Person]) [] 0 0
Michael
------------------------------
Message: 4
Date: Mon, 27 Jun 2011 09:50:51 +0200
From: Heinrich Apfelmus <[email protected]>
Subject: Re: [Haskell-beginners] A first try
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8; format=flowed
David Place wrote:
> On Jun 26, 2011, at 8:25 AM, Heinrich Apfelmus wrote:
>
>> What about the combinator
>>
>> withFile :: FilePath -> (String -> a) -> IO a
>> withFile name f = bracket (openFile name ReadMode) hClose $ \h ->
>> evaluate . f =<< hGetContents h
>>
>> ? It gives you the same thing as Iteratees - a way to apply a
>> function to the contents of a file - without the need to rewrite all the
>> existing list functions like map , lines , words , and so on.
>
> How would you, for instance, implement the program for counting all
> the words in a list of files that Oleg describes in his message?
>
>> http://okmij.org/ftp/Haskell/Iteratee/Lazy-vs-correct.txt
>
> Nested calls to withFile would require too many open handles.
Good point, but this actually shows that withFile should be even
lazier. In particular:
* The file should not be opened until the string is demanded.
* The file should be closed as soon as the string has been demanded in full.
As before, the idea is that resource usage is pushed into the
operational semantics and file handles are treated as if they were
ordinary lazy values. It's just that the operating system doesn't play
along very well and demands explicit resource management.
Again, let me stress that the biggest drawback of the Iteratee approach
is that you have to rewrite the consumer and cannot reuse ordinary list
functions like length , words , lines , and so on. But these functions
are already lazy, so why not make use of this. (You don't lose anything,
every Iteratee can be rewritten as an ordinary function String -> IO a
by using `seq` in corresponding places.)
Here one possibility for a lazier version of withFile' :
-- an even lazier version of withFile
withFile' :: FilePath -> (String -> IO a) -> IO a
withFile' name f = do
fin <- newIORef (return ())
let
close = readIORef fin >>= id
open = do
putStrLn "open"
h <- openFile name ReadMode
writeIORef fin (do putStrLn "close"; hClose h)
lazyRead h
finally (unsafeInterleaveIO open >>= f >>= evaluate) close
where
lazyRead h = hIsEOF h >>= \b ->
if b
then do putStrLn "close"; hClose h; return []
else do
c <- hGetChar h
cs <- unsafeInterleaveIO $ lazyRead h
return (c:cs)
withFiles :: [FilePath] -> (String -> IO a) -> IO a
withFiles [x] f = withFile' x f
withFiles (x:xs) f = withFile' x $ \s ->
let f' t = f (s ++ t) in withFiles xs f'
test = withFiles (replicate 200 "Test.hs") (return . length)
>>= print
Best regards,
Heinrich Apfelmus
--
http://apfelmus.nfshost.com
------------------------------
Message: 5
Date: Mon, 27 Jun 2011 15:53:34 +0800
From: Haisheng Wu <[email protected]>
Subject: [Haskell-beginners] Doing IO in terms of Arrow
To: Haskell Beginer <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hello,
I'm wondering if it is possible doing IO in terms of Arrow.
I got a function search all files under directory recursively and I'd like
to refartoring using Arrow terms.
I tried to define a function like below but failed.
isDirExist = Kleisli doesDirectoryExist
Do I need to define a instance for IO to be Arrow? Or is there any
existing solutions?
I'm a newbie of Haskell and thanks your help.
~~~~~
getFilesInDir :: FilePath -> IO [FilePath]
getFilesInDir inp = do
isDir <- doesDirectoryExist inp
files <- if isDir then
(do
names <- getDirectoryContents inp
forM [ inp </> x | x <- names, isNotSpecialDir x ]
getFilesInDir)
else return [[inp]]
return $ concat files
~~~~~
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110627/3fd02a31/attachment-0001.htm>
------------------------------
Message: 6
Date: Mon, 27 Jun 2011 08:25:32 +0000
From: Roelof Wobben <[email protected]>
Subject: [Haskell-beginners] book question
To: <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"
Hello, What's a good book for a absolute beginner in Haskell programming with
a lot of exercises so I can see if I understand it. Roelof
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20110627/99c769d3/attachment-0001.htm>
------------------------------
Message: 7
Date: Mon, 27 Jun 2011 11:15:13 +0200
From: Manfred Lotz <[email protected]>
Subject: Re: [Haskell-beginners] A first try
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=UTF-8
On Mon, 27 Jun 2011 12:39:42 +0900
Antoine Latter <[email protected]> wrote:
> On Mon, Jun 27, 2011 at 11:57 AM, Manfred Lotz
> <[email protected]> wrote:
> > On Sun, 26 Jun 2011 17:32:02 -0400
> > David Place <[email protected]> wrote:
> >
> >>
> >>
> >> On Jun 26, 2011, at 4:59 PM, Manfred Lotz wrote:
> >>
> >> > Is there any problem in the code snippet I pasted? If so I would
> >> > like to get a hint of course.
> >>
> >> There really isn't enough code in your snippet to be able to say.
> >> The rule is that if you return a lazy data structure from withFile
> >> you may find the file has been closed before you have read the
> >> data. If your parseXMLDoc is strict then you will get away with
> >> it. ? I feel that isn't very nice.
> >>
> >
> > What I had learned (and it seems to work fine) is to force the
> > result which gets done here in return $!
> >
> > insertXml (stat, m) xf =
> > ?U.withBinaryFile xf ReadMode
> > ? ? (\handle -> do ct <- getXmlContent xf handle
> > ? ? ? ? ?... some code...
> > ? ? ? ? return $! (stat',m'))
> >
> > So I hope that is the one and important recipe to add when using
> > withBinaryFile.
> >
>
> The operator ($!) isn't recursive, so all that it does is force the
> tuple you're returning, not the contents of the tuple.
>
Hmm, you are right.
> Internally it calls 'seq', which only forces its argument enough to
> determine which constructor it was called with. Sometimes this can be
> enough to do what you want, but not usually. And certainly not in this
> case.
>
I tried now:
getXmlContent :: String -> Handle -> IO Ctan
getXmlContent xf inh = do
xml <- U.hGetContents inh
let content = xml `deepseq` parseXMLDoc xml
case content of
...
deepseq really ensures parseXmlDoc gets the full file as a string.
It is unclear to me how this could be done without deepseq.
--
Manfred
------------------------------
Message: 8
Date: Mon, 27 Jun 2011 11:21:37 +0200
From: Raphael P?bst <[email protected]>
Subject: Re: [Haskell-beginners] book question
To: Roelof Wobben <[email protected]>
Cc: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
I used Real World Haskell
Http:/book.realworldhaskell.org
and was very happy with it.
There's also Learn You A Haskell For Great Good
http://www.learnyouahaskell.com
which is probably funnier, but I can't tell you much more about it.
For both it is helpful to have some experience with programming in
genereal, but these are the first that came to mind.
Raf
On 6/27/11, Roelof Wobben <[email protected]> wrote:
>
>
>
>
> Hello, What's a good book for a absolute beginner in Haskell programming
> with a lot of exercises so I can see if I understand it. Roelof
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 36, Issue 68
*****************************************