[Haskell-beginners] State Monad - Not in scope: data constructor `State'

Hugo Ferreira hmf at inescporto.pt
Mon Oct 31 10:38:11 CET 2011


On 10/29/2011 09:09 PM, Brent Yorgey wrote:
> On Sat, Oct 29, 2011 at 08:48:28PM +0100, Hugo Ferreira wrote:
>> Hello,
>>
>> I am trying to use some State Monad code
>> in order to learn about this. However in
>> GHCi I cannot use the "State" constructor.
>>
>> import qualified Control.Monad.State
>>
>>> :{
>> | let fromStoAandS c | c `mod` 5 == 0 = ("foo",c+1)
>> |                | otherwise      = ("bar",c+1)
>> | :}
>>> :t fromStoAandS
>>> fromStoAandS :: Integral t =>  t ->  ([Char], t)
>>
>>> :{
>> | let stateIntString = State fromStoAandS
>>> Prelude Control.Monad.State| :}
>>>
>>> <interactive>:1:22: Not in scope: data constructor `State'
>>
>> I have found a message stating that this is not possible
>> because their is "no State data constructor", it is only "defined
>> to be a type alias".
>
> That is correct.  State used to be defined directly; now it is just an
> alias for StateT s Identity.  You don't really have to worry about
> what this means, but the point is that there is no longer a real State
> constructor.
>
> The author of the message then says we should use:
>>
>> http://hackage.haskell.org/packages/archive/transformers/latest/doc/html/Control-Monad-Trans-State-Lazy.html#v:state
>>
>> instead. Still lost however.
>> So, how do I create the "Sate" above?
>
> You simply replace 'State' by 'state', a function provided to act like
> the old State constructor.  So you write
>
>    let stateIntString = state fromStoAandS

Ok, Got it.

Thanks,
Hugo F.

>
> -Brent
>
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners
>




More information about the Beginners mailing list