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. Lazy variant of sequence (or other way to approach problem)
(Nathan H?sken)
2. confused by <- (Bryce Verdier)
3. Re: Picking a random element from a Map (Harald B?geholz)
4. Re: Lazy variant of sequence (or other way to approach
problem) (Ertugrul S?ylemez)
5. Re: confused by <- (Ertugrul S?ylemez)
6. Re: confused by <- (Bryce Verdier)
7. Re: confused by <- (Ertugrul S?ylemez)
8. Re: Lazy variant of sequence (or other way to approach
problem) (Nathan H?sken)
----------------------------------------------------------------------
Message: 1
Date: Wed, 26 Sep 2012 17:41:57 +0200
From: Nathan H?sken <[email protected]>
Subject: [Haskell-beginners] Lazy variant of sequence (or other way to
approach problem)
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Hey,
In my (SDL based) haskell program, I do:
events <- liftM ( takeWhile (/= NoEvent)) $ sequence $ repeat pollEvent
The execution of this never returns, I am guessing that is because
sequence evaluation never stops.
But if sequence would be lazy (and assuming pollEvent returns NoEvent at
some point) this should stop, should it not?
Is there a lazy variant of sequence? Or am I missing something here
completely?
Thanks!
Nathan
------------------------------
Message: 2
Date: Wed, 26 Sep 2012 09:26:54 -0700
From: Bryce Verdier <[email protected]>
Subject: [Haskell-beginners] confused by <-
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1; format=flowed
I'm trying to play with a Yesod application. I had working code before,
but tried stretching myself a bit and adding in some error checking.
This code worked:
getGoogR = do
body <- simpleHttp "http://www.google.com"
jsonToRepJson $ object ["response" .= (showDigest $ sha1 body)]
This is the new code that is trying to use try (imported from
Control.Exception)
getGoogR = do
body <- try (simpleHttp "http://www.google.com") :: IO (Either
SomeException Data.ByteString.Lazy.Internal.ByteString)
case body of
Left _ -> jsonToRepJson $ object ["response" .= ( show "ERROR:
NO DATA FOR ONE REASON OR ANOTHER")]
Right val -> jsonToRepJson $ object ["response" .= (showDigest
$ sha1 val)]
and I get this error:
Couldn't match expected type `IO b0'
with actual type `GHandler sub0 master0 RepJson'
In the expression:
jsonToRepJson
$ object
["response" .= (show "ERROR: NO DATA FOR ONE REASON OR ANOTHER")]
In a case alternative:
Left _
-> jsonToRepJson
$ object
["response" .= (show "ERROR: NO DATA FOR ONE REASON OR
ANOTHER")]
In a stmt of a 'do' block:
case body of {
Left _
-> jsonToRepJson
$ object
["response" .= (show "ERROR: NO DATA FOR ONE REASON OR
ANOTHER")]
Right val
-> jsonToRepJson $ object ["response" .= (showDigest $ sha1
val)]
I'm sure that my problem has a simple fix, but I just don't know what it
is. I will be happy to share any other code if people ask, I'm just
giving the short version for brevities sake.
Thanks in advance,
Bryce
------------------------------
Message: 3
Date: Wed, 26 Sep 2012 23:06:54 +0200
From: Harald B?geholz <[email protected]>
Subject: Re: [Haskell-beginners] Picking a random element from a Map
To: [email protected]
Cc: Haskell Beginners <[email protected]>
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
Am 26.09.12 10:09, schrieb Daniel Trstenjak:
>
> Hi Harald,
>
>> how do I (quickly) pick a random element from a `Data.Map.Map`?
[..]
> M.keys map !! randomValue `mod` M.size map
>
> But if you need a high performance solution than it's more appropriate
> to put the values into a Data.Vector and indexing into it.
I was hoping to find a solution that makes use of the specific structure
of a map, but now I have written it like this and it works for me:
let (r, gen') = randomR (0, Map.size m - 1) gen
in (Map.keys m !! r, gen')
Thanks for the hint!
Harald
--
Harald B?geholz <[email protected]> (PGP key available from servers)
Redaktion c't Tel.: +49 511 5352-300 Fax: +49 511 5352-417
http://www.ct.de/
int f[9814],b,c=9814,g,i;long a=1e4,d,e,h;
main(){for(;b=c,c-=14;i=printf("%04d",e+d/a),e=d%a)
while(g=--b*2)d=h*b+a*(i?f[b]:a/5),h=d/--g,f[b]=d%g;}
(Arndt/Haenel)
Affe Apfel Vergaser
/* Heise Zeitschriften Verlag GmbH & Co. KG * Karl-Wiechert-Allee 10 *
30625 Hannover * Registergericht: Amtsgericht Hannover HRA 26709 *
Pers?nlich haftende Gesellschafterin: Heise Zeitschriften Verlag *
Gesch?ftsf?hrung GmbH * Registergericht: Amtsgericht Hannover, HRB
60405 * Gesch?ftsf?hrer: Ansgar Heise, Dr. Alfons Schr?der */
------------------------------
Message: 4
Date: Thu, 27 Sep 2012 01:45:08 +0200
From: Ertugrul S?ylemez <[email protected]>
Subject: Re: [Haskell-beginners] Lazy variant of sequence (or other
way to approach problem)
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="utf-8"
Nathan H?sken <[email protected]> wrote:
> In my (SDL based) haskell program, I do:
>
> events <- liftM ( takeWhile (/= NoEvent)) $ sequence $ repeat
> pollEvent
>
> The execution of this never returns, I am guessing that is because
> sequence evaluation never stops.
>
> But if sequence would be lazy (and assuming pollEvent returns NoEvent
> at some point) this should stop, should it not?
> Is there a lazy variant of sequence? Or am I missing something here
> completely?
The sequence function itself cannot be lazy, and there can't be a lazy
variant of it. What you want is unsafeLazySequenceIO, which uses
unsafeInterleaveIO under the hood, which is IO-specific. As always lazy
I/O is probably a bad idea and you should write a coroutine-based
combinator for that. The simplest way is the ad-hoc way:
pollEvent_ = do
ev <- pollEvent
case ev of
NoEvent -> pollEvent_
_ -> return ev
The coroutine-based method would look something like this:
import Control.Monad.Trans.Free -- from the 'free' package
newtype AppF a = AppF (Event -> a)
type App = FreeT AppF IO
Since FreeT is effectively just Coroutine from the monad-coroutine
package you can use that one instead with the 'Await Event' functor, but
the 'free' package provides a lot more useful instances. Your main loop
can then suspend to ask for the next event and the surrounding
application can provide the event in whatever way it wishes (for example
ignoring NoEvent):
myLoop = do
ev <- await
case ev of
Quit -> return ()
_ -> doSomethingWith ev
By the way, if your application is non-continuously rendered, which is
suggested by your ignoring of NoEvent, you shouldn't use pollEvent at
all. Rather you should use waitEvent, which blocks instead of returning
NoEvent. That way you don't waste precious CPU cycles. The pollEvent
action is meant for applications that are continuously rendered, where
you would e.g. perform drawing when you get NoEvent.
Hope this helps.
Greets,
Ertugrul
--
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20120927/b615eba6/attachment-0001.pgp>
------------------------------
Message: 5
Date: Thu, 27 Sep 2012 01:53:23 +0200
From: Ertugrul S?ylemez <[email protected]>
Subject: Re: [Haskell-beginners] confused by <-
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
Bryce Verdier <[email protected]> wrote:
> This is the new code that is trying to use try (imported from
> Control.Exception)
> getGoogR = do
> body <- try {- [...] -}
>
> [...]
>
> I'm sure that my problem has a simple fix, but I just don't know what
> it is. I will be happy to share any other code if people ask, I'm just
> giving the short version for brevities sake.
The fix is indeed simple. In fact you're just using the wrong 'try',
because Yesod handlers aren't IO actions, but actions in a monad that is
specific to your application, called Handler. The error message
suggests that, although the type is spelled out completely there.
What you want is the lifted version of 'try', which you find in the
Control.Exception.Lifted module.
Greets,
Ertugrul
--
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20120927/5bde9795/attachment-0001.pgp>
------------------------------
Message: 6
Date: Wed, 26 Sep 2012 17:31:46 -0700
From: Bryce Verdier <[email protected]>
Subject: Re: [Haskell-beginners] confused by <-
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="iso-8859-1"; Format="flowed"
On 9/26/12 4:53 PM, Ertugrul S?ylemez wrote:
> Bryce Verdier <[email protected]> wrote:
>
>> This is the new code that is trying to use try (imported from
>> Control.Exception)
>> getGoogR = do
>> body <- try {- [...] -}
>>
>> [...]
>>
>> I'm sure that my problem has a simple fix, but I just don't know what
>> it is. I will be happy to share any other code if people ask, I'm just
>> giving the short version for brevities sake.
> The fix is indeed simple. In fact you're just using the wrong 'try',
> because Yesod handlers aren't IO actions, but actions in a monad that is
> specific to your application, called Handler. The error message
> suggests that, although the type is spelled out completely there.
>
> What you want is the lifted version of 'try', which you find in the
> Control.Exception.Lifted module.
>
>
> Greets,
> Ertugrul
Thank you for the reply Ertugrul, sadly that didn't seem to help. :(
Here are my changes:
import qualified Control.Exception.Lifted as L
...
body <- L.try (simpleHttp "http://www.google.com") :: IO (Either
L.SomeException Data.ByteString.Lazy.Internal.ByteString)
I did the full qualified because the first time I tried your suggestion
I just "Control.Exception.Lifted" and I got the same error as I'm
sharing below.
playhaven.hs:51:19:
Couldn't match expected type `IO b0'
with actual type `GHandler sub0 master0 RepJson'
In the expression:
Thanks for being willing to help me with this.
Bryce
>
>
>
> _______________________________________________
> 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/20120926/ceaee65d/attachment-0001.htm>
------------------------------
Message: 7
Date: Thu, 27 Sep 2012 03:00:37 +0200
From: Ertugrul S?ylemez <[email protected]>
Subject: Re: [Haskell-beginners] confused by <-
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset="us-ascii"
Bryce Verdier <[email protected]> wrote:
> body <- L.try (simpleHttp "http://www.google.com") :: IO (Either
> L.SomeException Data.ByteString.Lazy.Internal.ByteString)
Almost right, but your type signature is lying. =)
Greets,
Ertugrul
--
Not to be or to be and (not to be or to be and (not to be or to be and
(not to be or to be and ... that is the list monad.
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: not available
URL:
<http://www.haskell.org/pipermail/beginners/attachments/20120927/85966d53/attachment-0001.pgp>
------------------------------
Message: 8
Date: Thu, 27 Sep 2012 09:14:27 +0200
From: Nathan H?sken <[email protected]>
Subject: Re: [Haskell-beginners] Lazy variant of sequence (or other
way to approach problem)
To: [email protected]
Message-ID: <[email protected]>
Content-Type: text/plain; charset=ISO-8859-1
On 09/27/2012 01:45 AM, Ertugrul S?ylemez wrote:
> Nathan H?sken <[email protected]> wrote:
>
>> In my (SDL based) haskell program, I do:
>>
>> events <- liftM ( takeWhile (/= NoEvent)) $ sequence $ repeat
>> pollEvent
>>
>> The execution of this never returns, I am guessing that is because
>> sequence evaluation never stops.
>>
>> But if sequence would be lazy (and assuming pollEvent returns NoEvent
>> at some point) this should stop, should it not?
>> Is there a lazy variant of sequence? Or am I missing something here
>> completely?
>
> The sequence function itself cannot be lazy, and there can't be a lazy
> variant of it.
I understand, that it is a bad Idea. But why is it impossible to have an
lazy sequence? Why can it not wait with the execution of the action for
the list elements to be evaluated?
> By the way, if your application is non-continuously rendered, which is
> suggested by your ignoring of NoEvent, you shouldn't use pollEvent at
> all.
The Idea of the line what to return all events that happened in the
current frame (which are to my understanding all events until pollEvent
returns NoEvent).
> Hope this helps.
Yes, thank you. I have to read into the Coroutine approach a little more
before to understand it :).
Best Regards,
Nathan
------------------------------
_______________________________________________
Beginners mailing list
[email protected]
http://www.haskell.org/mailman/listinfo/beginners
End of Beginners Digest, Vol 51, Issue 39
*****************************************