[Haskell-beginners] Grappling with State Monad get

Olumide 50295 at web.de
Wed Aug 2 00:13:12 UTC 2017


Of course 's' is not a type variable as its lowercase.

Therefore, get is a monad 'constructed' by the state function, correct?

So, in the do notation the lambda is extracted and used as per the 
definition of bind. The context of which we speak therefore must derive 
from the next expression(?) in the do notation, which is somewhat 
confusing to determine in the example

     stackyStack :: State Stack ()
     stackyStack = do
         stackNow <- get
         if stackNow == [1,2,3]
             then put [8,3,1]
             else put [9,2,1]

Regards,

- Olumide

On 02/08/17 00:35, Theodore Lief Gannon wrote:
> 's' here is not a type variable, it's an actual variable. \s -> (s, s) 
> defines a lambda function which takes any value, and returns a tuple 
> with that value in both positions.
> 
> So yes, get is point-free, but the missing argument is right there 
> in-line. And yes, its type is simply implied from context.
> 
> 
> On Aug 1, 2017 4:17 PM, "Olumide" <50295 at web.de <mailto:50295 at web.de>> 
> wrote:
> 
>     Ahoy Haskellers,
> 
>     In the section "Getting and Setting State"
>     (http://learnyouahaskell.com/for-a-few-monads-more#state
>     <http://learnyouahaskell.com/for-a-few-monads-more#state>) in LYH
>     get is defined as
> 
>     get = state $ \s -> (s, s)
> 
>     How does does get determine the type s, is considering that it has
>     no argument as per the definition given above? Or is the definition
>     written in some sort of point-free notation where an argument has
>     been dropped?
> 
>     I find the line stackNow <- get in the the function(?) stackyStack
>     confusing for the same reason. I guess my difficulty is that state $
>     \s ->(s , s) has a generic type (s) whereas the stackyStack has a
>     concrete type. Is the type of s determined from the type of the
>     stateful computation/do notation?
> 
>     Regards,
> 
>     - Olumide
>     _______________________________________________
>     Beginners mailing list
>     Beginners at haskell.org <mailto:Beginners at haskell.org>
>     http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
>     <http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners>
> 
> 
> 
> 
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
> 


More information about the Beginners mailing list