Re: [Haskell-beginners] Beginners Digest, Vol 56, Issue 33

Hi All: How do you define a function of signature h :: M Int -> M Int -> M Int so that h ( M x ) ( M y ) = M ( x + y ), but without unwrapping the value from the monad? This question is from the article "Trivial Monad" found at http://blog.sigfpe.com/2007/04/trivial-monad.html. The provided answer is h x y = x >>= (\x -> g x y) or equivalently ( in context of the article ) h :: M Int -> M Int -> M Int h x y = bind ( \x-> g x y ) x where g is g :: Int -> W Int -> W Int g x y = y >>= (return . (+x)) for the monad: data M a = M a deriving Show Now I am a little confused, how can you put x in g if it takes an Int as first parameter but x is M Int? Thanks!

On Fri, Feb 22, 2013 at 7:27 PM, xiao Ling
h :: M Int -> M Int -> M Int h x y = bind ( \x-> g x y ) x
where g is
g :: Int -> W Int -> W Int g x y = y >>= (return . (+x))
for the monad:
data M a = M a deriving Show
Now I am a little confused, how can you put x in g if it takes an Int as first parameter but x is M Int?
Because it's a different "x". Lemme rewrite it slightly: h :: M Int -> M Int -> M Int h x y = bind ( \w -> g w y ) x All I did was replace the inner "x" with "w", to demonstrate that it has no relationship to the outer "x"; the \... -> syntax introduces new local bindings unrelated to any outside of it, in this case for "w" (or what he had "x", shadowing the original binding of "x" within the lambda). -- brandon s allbery kf8nh sine nomine associates allbery.b@gmail.com ballbery@sinenomine.net unix, openafs, kerberos, infrastructure, xmonad http://sinenomine.net

On Fri, Feb 22, 2013 at 07:27:28PM -0500, xiao Ling wrote:
Hi All: How do you define a function of signature h :: M Int -> M Int -> M Int so that h ( M x ) ( M y ) = M ( x + y ), but without unwrapping the value from the monad?
This question is from the article "Trivial Monad" found at http://blog.sigfpe.com/2007/04/trivial-monad.html. The provided answer is
h x y = x >>= (\x -> g x y)
Now I am a little confused, how can you put x in g if it takes an Int as first parameter but x is M Int?
I think your confusion may stem from the fact that there are *two different* things named 'x' in the above code. The x's on the right hand side of the >>= shadow the x on the left hand side. This is confusing and poor style; a better way to write it would be h x y = x >>= (\i -> g i y) If you study the type of >>= you will see that i indeed has type Int, as required for the first argument of g. -Brent

On Sat, Feb 23, 2013 at 7:27 AM, xiao Ling
How do you define a function of signature h :: M Int -> M Int -> M Int so that h ( M x ) ( M y ) = M ( x + y ), but without unwrapping the value from the monad?
In addition to the fine points that Brent and Brandon have already made, I observe that there seems to be a reservation about "unwrapping the value from the monad", which I don't get.
Your code is equivalent to h = \mx my -> do { x <- mx; y <- my; return $ x+y; }, which, I suspect, doesn't go well with you because of the "unwrapping". Or are you aiming at syntactic compositionality, i.e. point-free style? Once you've hacked Haskell enough, you just reach for the liftM2 combinator and write h = liftM2 (+). -- Kim-Ee

Hi, Why GHC doesn't tell me such things like hpaste? (example here: http://hpaste.org/82917) 27:12: Error: Use replicateM Found: sequence $ replicate gridSize getLine Why not: Control.Monad.replicateM gridSize getLine 50:14: Warning: Use list comprehension Found: if y > 0 then [(UP, (x, y - 1))] else [] Why not: [(UP, (x, y - 1)) | y > 0] 81:1: Error: Eta reduce Found: sortByPathCost p ps = sortBy compareHeuristic ps Why not: sortByPathCost p = sortBy compareHeuristic Can I enable this kind of verbosity in GHC somehow? Emanuel

It's hlint: http://community.haskell.org/~ndm/hlint/ e.g.: # sudo apt-get install hlint # hlint src (where src is the folder containing Haskell files) or # hlint src --report to generate an HTML report. On Sat, Feb 23, 2013 at 10:25 PM, Emanuel Koczwara < poczta@emanuelkoczwara.pl> wrote:
Hi,
Why GHC doesn't tell me such things like hpaste? (example here: http://hpaste.org/82917)
27:12: Error: Use replicateM Found: sequence $ replicate gridSize getLine Why not: Control.Monad.replicateM gridSize getLine
50:14: Warning: Use list comprehension Found: if y > 0 then [(UP, (x, y - 1))] else [] Why not: [(UP, (x, y - 1)) | y > 0]
81:1: Error: Eta reduce Found: sortByPathCost p ps = sortBy compareHeuristic ps Why not: sortByPathCost p = sortBy compareHeuristic
Can I enable this kind of verbosity in GHC somehow?
Emanuel
_______________________________________________ Beginners mailing list Beginners@haskell.org http://www.haskell.org/mailman/listinfo/beginners

On Sat, Feb 23, 2013 at 10:58 PM, Kim-Ee Yeoh
On Sat, Feb 23, 2013 at 7:27 AM, xiao Ling
wrote: How do you define a function of signature h :: M Int -> M Int -> M Int so that h ( M x ) ( M y ) = M ( x + y ), but without unwrapping the value from the monad?
there seems to be a reservation about "unwrapping the value from the monad", which I don't get.
It's been pointed out to me that I've completely missed OP's question (which is really about variable names and their scope), my bad. OP linked to sigfpe's article, which is what I should have been critiquing. Dan has contributed a lot of good material about Haskell, and I follow his blog closely. But the Trivial Monad article really isn't one of his best, and he perpetuates the faulty "wrapping" metaphor that seriously hampers Haskell productivity. I'll write later explaining why and provide a better substitute. -- Kim-Ee
participants (6)
-
Brandon Allbery
-
Brent Yorgey
-
Emanuel Koczwara
-
Kim-Ee Yeoh
-
Patrick Mylund Nielsen
-
xiao Ling