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. exercises/examples for streams (Pascal Knodel)
2. Re: Buggy behavior of "withFile" (Kim-Ee Yeoh)
3. Re: Buggy behavior of "withFile" (Kim-Ee Yeoh)
4. Re: exercises/examples for streams (Carl Eyeinsky)
----------------------------------------------------------------------
Message: 1
Date: Fri, 12 Dec 2014 04:14:47 +0100
From: Pascal Knodel <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] exercises/examples for streams
Message-ID: <[email protected]>
Content-Type: text/plain; charset=utf-8; format=flowed
Hello,
I'm preparing for an exam (university level introductory course).
Functional programming in Haskell is part of it. That includes the
concept of streams (theoretically infinite list computations).
I have difficulties in solving exercises that ask me to define streams
for a given problem. Do you know good examples, the difficulty should
be maybe a little harder than Fibonacci or intersections, but solvable in
about 5 to 10 minutes. If there are examples from the book "Haskell -
the craft of functional programming", please don't spoiler, I'm reading
it parallel to this conversation. Don't give solutions, just problems (5 to
10 more will do). Examples, other than integer sequences are welcome.
Pascal
------------------------------
Message: 2
Date: Fri, 12 Dec 2014 10:29:03 +0700
From: Kim-Ee Yeoh <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Buggy behavior of "withFile"
Message-ID:
<CAPY+ZdRAEULTDGq5XY58urxhKK0KrPuY0dHMrcnq8dDomPK=z...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Wed, Dec 10, 2014 at 3:34 AM, Christopher Reichert <[email protected]
> wrote:
>
> Google around for "haskell lazy io withFile" and you might find more
> detailed explanations.
>
Yes indeed. The explanations are pretty good too.
This is a gotcha that deserves to be more widely known. A recent patch to
ghc gives a better error message explaining what's going on.
How does one go about using withFile correctly then?
If you see the code "withFile filename ReadMode" chances are you should
replace it with "readFile filename".
Hence, instead of
main = do
cnts <- withFile "example.hs" ReadMode $ (\h -> do
res <- hGetContents h
--putStr res
return res)
putStr cnts
write
main = do
cnts <- readFile "example.hs"
putStr cnts
or more idiomatically
main = readFile "example.hs" >>= putStr
-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20141212/5565c033/attachment-0001.html>
------------------------------
Message: 3
Date: Fri, 12 Dec 2014 15:36:32 +0700
From: Kim-Ee Yeoh <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] Buggy behavior of "withFile"
Message-ID:
<CAPY+ZdTaxgfiEJRVV5rd+6-7zYDWmyOo6DmFCRK=jpbkc5d...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
On Wed, Dec 10, 2014 at 3:36 AM, Mike Meyer <[email protected]> wrote:
>
> If I'm right, you need to force the evaluation of res before the return,
> with something like "res seq return res" instead of just "return res". Or
> use the BangPatterns extension and then write "!res <- hGetContents h"
This will read one byte (or none at all if the file is empty).
That one byte will then be displayed by the putStr outside the withFile
expression. It's a small but noticeable improvement to nothing being
displayed at all.
Why one byte? Because seq evaluates only to weak head normal form (WHNF),
which in the case of a list, amounts to determining whether it's [] or
(x:xs).
You could write
res <- hGetContents h
evaluate $ length res -- Control.Exception.evaluate
return res
but if you're going to do that, you're better off using Strict I/O. And in
fact, strict _bytestring_ I/O because the spatial footprint of lists is 10x
bytestrings.
But really, the default built-in lazy I/O on standard lists are fine for
general usage. Avoid premature optimization.
-- Kim-Ee
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20141212/016d4f91/attachment-0001.html>
------------------------------
Message: 4
Date: Fri, 12 Dec 2014 09:40:36 +0100
From: Carl Eyeinsky <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] exercises/examples for streams
Message-ID:
<cadkv3agubvwkjmstpaovqt-d45jdzqv8khzwq-dzugjw1gd...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hi, Pascal
(and hi, list!)
1. Here is one: define a function that takes two lists, both of which have
elements in growing order (i.e 1,2,3,3,5,9,9,..), and outputs a list with
the type of [Either a a], where a `Left a' would represent an element from
the first list, a `Right a' from the second list. Make it so that the
elements are in order, and also that the lefts preceede the rights.
Example, the inputs of [1,1,3,7,7] and [1,2,3,5,5] would result in [Left 1,
Left 1, Right 1, Right 2, Left 3, Right 3, Right 5, Right 5, Left 7, Left 7]
2. As an extension (not sure if this is too difficult for what you're
looking for): write another function that takes a list-of-lists as input
and outputs the result in tuples, where the first member is the index of
the list (counting from zero), and the second is the value itself.
Example [[1,3],[2,3],[1,3,4]] should result in
[(0,1),(2,1),(1,2),(0,3),(1,3),(2,3),(2,4)]
On Fri, Dec 12, 2014 at 4:14 AM, Pascal Knodel <[email protected]>
wrote:
>
> Hello,
>
> I'm preparing for an exam (university level introductory course).
> Functional programming in Haskell is part of it. That includes the
> concept of streams (theoretically infinite list computations).
>
> I have difficulties in solving exercises that ask me to define streams
> for a given problem. Do you know good examples, the difficulty should
> be maybe a little harder than Fibonacci or intersections, but solvable in
> about 5 to 10 minutes. If there are examples from the book "Haskell -
> the craft of functional programming", please don't spoiler, I'm reading
> it parallel to this conversation. Don't give solutions, just problems (5 to
> 10 more will do). Examples, other than integer sequences are welcome.
>
> Pascal
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://www.haskell.org/mailman/listinfo/beginners
>
--
Carl Eyeinsky
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20141212/ae5315ae/attachment-0001.html>
------------------------------
Subject: Digest Footer
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
------------------------------
End of Beginners Digest, Vol 78, Issue 6
****************************************