
In the code below, is the type returned by the return functions inferred from the result type in the function type signature, i.e., just change the result type to Maybe Int and the code will return a Maybe monad, (Just 4), instead of a List monad? Michael ========= import Monad fn :: [Int] -> [Int] fn l = mzero `mplus` (return (head l)) `mplus` (return (last l)) ================ *Main> :l test5 [1 of 1] Compiling Main ( test5.hs, interpreted ) Ok, modules loaded: Main. *Main> fn [4,5,6,7,8] [4,8] *Main>

michael rice
In the code below, is the type returned by the return functions inferred from the result type in the function type signature, i.e., just change the result type to Maybe Int and the code will return a Maybe monad, (Just 4), instead of a List monad?
Yes. Prelude> :m Control.Monad Prelude Control.Monad> let fn l = mzero `mplus` (return (head l)) `mplus` (return (last l)) Prelude Control.Monad> fn [1,3] :: Maybe Int Just 1 Prelude Control.Monad> fn [1,3] :: Maybe Float Just 1.0 Prelude Control.Monad> fn [1,2,3] :: [Int] [1,3] -- (c) this sig last receiving data processing entity. Inspect headers for copyright history. All rights reserved. Copying, hiring, renting, performance and/or quoting of this signature prohibited.

On Mon, May 11, 2009 at 10:59:01PM -0700, michael rice wrote:
In the code below, is the type returned by the return functions inferred from the result type in the function type signature, i.e., just change the result type to Maybe Int and the code will return a Maybe monad, (Just 4), instead of a List monad?
Indeed, it is. Try it! =) -Brent

Am Dienstag 12 Mai 2009 07:59:01 schrieb michael rice:
In the code below, is the type returned by the return functions inferred from the result type in the function type signature, i.e., just change the result type to Maybe Int and the code will return a Maybe monad, (Just 4), instead of a List monad?
You can find out such things yourself, just remove the type signature and ask ghci/hugs what they think: *MType> :t fn fn :: (MonadPlus m) => [a] -> m a
Michael
=========
import Monad
fn :: [Int] -> [Int] fn l = mzero `mplus` (return (head l)) `mplus` (return (last l))
================
*Main> :l test5 [1 of 1] Compiling Main ( test5.hs, interpreted ) Ok, modules loaded: Main. *Main> fn [4,5,6,7,8] [4,8] *Main>
participants (4)
-
Achim Schneider
-
Brent Yorgey
-
Daniel Fischer
-
michael rice