[Haskell-beginners] State Monad
Mike Meyer
mwm at mired.org
Fri Apr 17 23:34:30 UTC 2015
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 <tjakway at nyu.edu> 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 <tjakway at nyu.edu> 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 listBeginners at haskell.orghttp://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>
>
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> 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.html>
More information about the Beginners
mailing list