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: State Monad (Mike Meyer)
2. GCJ 2015 - Round 1A Problem - Haircut (Sourabh)
3. StateT MyState IO a vs ReaderT (IORef MyState) IO a
(Konstantin Saveljev)
4. Re: GCJ 2015 - Round 1A Problem - Haircut
(Sumit Sahrawat, Maths & Computing, IIT (BHU))
----------------------------------------------------------------------
Message: 1
Date: Fri, 17 Apr 2015 18:34:30 -0500
From: Mike Meyer <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] State Monad
Message-ID:
<CAD=7u2bqx9zuw0swzamwkzap_g0g42sreha40ezqnur9a+z...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Ok, now the analysis. Without the $, you get this error message (ghci
7.8.3):
Couldn't match type ?g0 -> (a0, g0)? with ?(a, (a0, a0))?
Expected type: (a0, a0) -> (a, (a0, a0))
Actual type: (a0, a0) -> g0 -> (a0, g0)
Relevant bindings include
genThree :: t -> a (bound at /tmp/test.hs:6:1)
Probable cause: ?randomR? is applied to too few arguments
In the first argument of ?state?, namely ?randomR?
In a stmt of a 'do' block: state randomR (listMin, listMax)
The key line is "Probable cause: 'randomR' is applied to too few
arguments". Since it takes and apparently has one argument, the most likely
cause is that the expression isn't parsed the way you expect.. So make the
parse explicit, which would be "state (randomR (listMin, listMax))". Using
the $ operator does the same thing, and reads a bit cleaner once you get
used to it.
On Fri, Apr 17, 2015 at 6:19 PM, Thomas Jakway <[email protected]> wrote:
> Thank you very much!
>
> On 4/17/15 6:48 PM, Mike Meyer wrote:
>
>
> On Fri, Apr 17, 2015 at 2:25 PM, Thomas Jakway <[email protected]> wrote:
>
>> genThree listMax = do --highest index in the list
>> let listMin = 0 :: Int --lowest index in the list
>> generatedMin <- state randomR (listMin, listMax)
>> return generatedMin
>>
>
> What you're missing is a $:
>
> The only chagne to our genThree functions is making it "state $" instead
> of "state".
>
>
> #!/usr/bin/env runhaskell
>
> import System.Random
> import Control.Monad.State
>
> genThree listMax = do --highest index in the list
> let listMin = 0 :: Int --lowest index in the list
> generatedMin <- state $ randomR (listMin, listMax)
> return generatedMin
>
> main = do
> gen <- newStdGen
> print $ evalState (genThree 10) gen
>
>
>
> _______________________________________________
> Beginners mailing
> [email protected]http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
>
> _______________________________________________
> 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/20150417/db19804a/attachment-0001.html>
------------------------------
Message: 2
Date: Fri, 17 Apr 2015 20:41:41 -0700
From: Sourabh <[email protected]>
To: [email protected]
Subject: [Haskell-beginners] GCJ 2015 - Round 1A Problem - Haircut
Message-ID:
<CAK0HM3=1w_rm45fiqxgqvwq22z4hzj8nwj5ffgtypuuavm+...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
So I decided to try the Google Code Jam this year, and decided to code in
Haskell.
I passed the qualifiers, and today was Round 1A. I managed to get the first
problem right (both small and large input), and got the 2nd problem right
(for the small input). However, for the large input, it ran out of stack
space with the following error:
Stack space overflow: current size 33632 bytes
Use '+RTS -Ksize -RTS' to increase it.
Can someone tell me how I could have avoided or fixed this?
It is also very possible that I had an algorithmic issue (needed to
optimize my code or have a different algorithm for the large input
perhaps?).
Here is the problem description:
https://code.google.com/codejam/contest/4224486/dashboard#s=p1
And here is my code:
https://github.com/cbrghostrider/Hacking/blob/master/codeJam/2015/haircut/haircut_try2.hs
I made sure to use foldr, which is supposedly more stack efficient I
believe. I don't know if the list append (++) was causing issues.
If anyone can spot any problems, or provide suggestions, I would really
appreciate it! Even though the round is done, I'd like to optimize this to
make it run within 8 minutes, as intended.
Thanks in advance!
--
SSJ
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150417/a07ec699/attachment-0001.html>
------------------------------
Message: 3
Date: Sat, 18 Apr 2015 12:04:30 +0300
From: Konstantin Saveljev <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] StateT MyState IO a vs ReaderT (IORef
MyState) IO a
Message-ID:
<cakncejotejamskbtzp3hf1gc7k6p+jfzvixy6idp5e0emmh...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Hello,
I'm working on a project where I use StateT MyState IO a to keep track of
the state. MyState is deeply nested and I use lens to help me more easily
access and modify the state.
Just about yesterday I found someone mentioning about using ReaderT (IORef
MyState) IO a instead of StateT MyState IO a because we are already in IO.
Can someone explain to me what are the benefits of these different
approaches? What about Garbage Collection in both cases (if there is any
difference at all)? Can you use lens package to access and update the state
which is hidden behind IORef ?
Thanks,
Konstantin Saveljev
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150418/5676fcb6/attachment-0001.html>
------------------------------
Message: 4
Date: Sat, 18 Apr 2015 15:29:00 +0530
From: "Sumit Sahrawat, Maths & Computing, IIT (BHU)"
<[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
beginner-level topics related to Haskell <[email protected]>
Subject: Re: [Haskell-beginners] GCJ 2015 - Round 1A Problem - Haircut
Message-ID:
<CAJbEW8PMjiXLVKUNpqqS0yHKnoK_RwOxS4QbA=m7cqrnm9z...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"
Take a look here: https://www.haskell.org/haskellwiki/Foldr_Foldl_Foldl'
Foldr is not recommended in most cases, the above article explains it all.
On 18 April 2015 at 09:11, Sourabh <[email protected]> wrote:
> So I decided to try the Google Code Jam this year, and decided to code in
> Haskell.
>
> I passed the qualifiers, and today was Round 1A. I managed to get the
> first problem right (both small and large input), and got the 2nd problem
> right (for the small input). However, for the large input, it ran out of
> stack space with the following error:
> Stack space overflow: current size 33632 bytes
> Use '+RTS -Ksize -RTS' to increase it.
>
> Can someone tell me how I could have avoided or fixed this?
>
> It is also very possible that I had an algorithmic issue (needed to
> optimize my code or have a different algorithm for the large input
> perhaps?).
>
> Here is the problem description:
> https://code.google.com/codejam/contest/4224486/dashboard#s=p1
>
> And here is my code:
>
> https://github.com/cbrghostrider/Hacking/blob/master/codeJam/2015/haircut/haircut_try2.hs
>
>
> I made sure to use foldr, which is supposedly more stack efficient I
> believe. I don't know if the list append (++) was causing issues.
>
> If anyone can spot any problems, or provide suggestions, I would really
> appreciate it! Even though the round is done, I'd like to optimize this to
> make it run within 8 minutes, as intended.
>
> Thanks in advance!
> --
> SSJ
>
> _______________________________________________
> Beginners mailing list
> [email protected]
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
--
Regards
Sumit Sahrawat
-------------- next part --------------
An HTML attachment was scrubbed...
URL:
<http://mail.haskell.org/pipermail/beginners/attachments/20150418/ee23b7c8/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 82, Issue 22
*****************************************