No subject


Thu Feb 24 17:58:36 CET 2011


mapM mf xs takes a monadic function mf (having type Monad m => (a -> m b))
and applies it to each element in list xs; the result is a list inside a
monad. 
 
A few things I've found:
mapM :: (a -> m b) -> [a] -> m [b]
So in this case: a = Maybe Int (second arg in mapM id [Just1, Just 2, Just
3] and b = Int and m = Maybe. So id is :: Maybe Int -> Maybe Int
 
mapM id [Just 1, Nothing, Just 3] 
result: Nothing.
My first guess for the result: Just [Just 1, Nothing, Just 3]
 
when I do: mapM id [1,2,3] I get an error (id has wrong type, which makes
sense)
 
Can somebody explain what is going on here?
 
Kees

------=_NextPart_000_0000_01CC16FA.AB6D8BE0
Content-Type: text/html;
	charset="us-ascii"
Content-Transfer-Encoding: quoted-printable

<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.0 Transitional//EN">
<HTML><HEAD>
<META content=3D"text/html; charset=3Dus-ascii" =
http-equiv=3DContent-Type>
<META name=3DGENERATOR content=3D"MSHTML 8.00.6001.19046"></HEAD>
<BODY>
<DIV><FONT size=3D2 face=3DArial><SPAN class=3D156020112-20052011>I was =
playing with=20
winghci and I tried:</SPAN></FONT></DIV>
<DIV><FONT size=3D2 face=3DArial><SPAN=20
class=3D156020112-20052011></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2 face=3DArial><SPAN class=3D156020112-20052011>mapM =
id [Just 1,=20
Just 2, Just 3]</SPAN></FONT></DIV>
<DIV><FONT size=3D2 face=3DArial><SPAN =
class=3D156020112-20052011>result: Just=20
[1,2,3]</SPAN></FONT></DIV>
<DIV><FONT size=3D2 face=3DArial><SPAN=20
class=3D156020112-20052011></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2 face=3DArial><SPAN class=3D156020112-20052011>I =
don't understand=20
this answer.</SPAN></FONT></DIV>
<DIV><FONT size=3D2 face=3DArial><SPAN=20
class=3D156020112-20052011></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2 face=3DArial><SPAN class=3D156020112-20052011>From =
<A=20
href=3D"http://members.chello.nl/hjgtuyl/tourdemonad.html#mapM"><FONT =
size=3D3=20
face=3D"Times New =
Roman">http://members.chello.nl/hjgtuyl/tourdemonad.html#mapM</FONT></A><=
/SPAN></FONT></DIV>
<DIV><FONT size=3D2 face=3DArial><SPAN class=3D156020112-20052011><SPAN=20
style=3D"WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; =
BORDER-COLLAPSE: separate; FONT: medium 'Times New Roman'; WHITE-SPACE: =
normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); =
WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; =
-webkit-border-vertical-spacing: 0px; =
-webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: =
auto; -webkit-text-stroke-width: 0px"=20
class=3DApple-style-span><SPAN=20
style=3D"FONT-FAMILY: Times, serif; -webkit-border-horizontal-spacing: =
5px; -webkit-border-vertical-spacing: 5px"=20
class=3DApple-style-span><SPAN style=3D"FONT: 12pt Courier, monospace; =
COLOR: green"=20
class=3Dcode>mapM mf xs </SPAN>takes a monadic function<SPAN=20
class=3DApple-converted-space>&nbsp;</SPAN><SPAN=20
style=3D"FONT: 12pt Courier, monospace; COLOR: green" =
class=3Dcode>mf</SPAN><SPAN=20
class=3DApple-converted-space>&nbsp;</SPAN>(having type<SPAN=20
class=3DApple-converted-space>&nbsp;</SPAN><SPAN=20
style=3D"FONT: 12pt Courier, monospace; COLOR: green" class=3Dcode>Monad =
m =3D&gt; (a=20
-&gt; m b)</SPAN>) and applies it to each element in list<SPAN=20
class=3DApple-converted-space>&nbsp;</SPAN><SPAN=20
style=3D"FONT: 12pt Courier, monospace; COLOR: green" =
class=3Dcode>xs</SPAN>; the=20
result is a list inside a monad.<SPAN=20
class=3DApple-converted-space>&nbsp;</SPAN></SPAN></SPAN></SPAN></FONT></=
DIV>
<DIV><FONT size=3D2 face=3DArial><SPAN class=3D156020112-20052011><SPAN=20
style=3D"WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; =
BORDER-COLLAPSE: separate; FONT: medium 'Times New Roman'; WHITE-SPACE: =
normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); =
WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; =
-webkit-border-vertical-spacing: 0px; =
-webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: =
auto; -webkit-text-stroke-width: 0px"=20
class=3DApple-style-span><SPAN=20
style=3D"FONT-FAMILY: Times, serif; -webkit-border-horizontal-spacing: =
5px; -webkit-border-vertical-spacing: 5px"=20
class=3DApple-style-span><SPAN=20
class=3DApple-converted-space></SPAN></SPAN></SPAN></SPAN></FONT>&nbsp;</=
DIV>
<DIV><FONT size=3D2 face=3DArial><SPAN class=3D156020112-20052011><SPAN=20
style=3D"WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; =
BORDER-COLLAPSE: separate; FONT: medium 'Times New Roman'; WHITE-SPACE: =
normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); =
WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; =
-webkit-border-vertical-spacing: 0px; =
-webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: =
auto; -webkit-text-stroke-width: 0px"=20
class=3DApple-style-span><SPAN=20
style=3D"FONT-FAMILY: Times, serif; -webkit-border-horizontal-spacing: =
5px; -webkit-border-vertical-spacing: 5px"=20
class=3DApple-style-span><SPAN class=3DApple-converted-space>
<DIV><FONT size=3D2 face=3DArial><SPAN class=3D156020112-20052011>A few =
things I've=20
found:</SPAN></FONT></DIV></SPAN></SPAN></SPAN></SPAN></FONT></DIV>
<DIV><FONT size=3D2 face=3DArial><SPAN class=3D156020112-20052011><SPAN=20
style=3D"WIDOWS: 2; TEXT-TRANSFORM: none; TEXT-INDENT: 0px; =
BORDER-COLLAPSE: separate; FONT: medium 'Times New Roman'; WHITE-SPACE: =
normal; ORPHANS: 2; LETTER-SPACING: normal; COLOR: rgb(0,0,0); =
WORD-SPACING: 0px; -webkit-border-horizontal-spacing: 0px; =
-webkit-border-vertical-spacing: 0px; =
-webkit-text-decorations-in-effect: none; -webkit-text-size-adjust: =
auto; -webkit-text-stroke-width: 0px"=20
class=3DApple-style-span><SPAN=20
style=3D"FONT-FAMILY: Times, serif; -webkit-border-horizontal-spacing: =
5px; -webkit-border-vertical-spacing: 5px"=20
class=3DApple-style-span><SPAN class=3DApple-converted-space>
<DIV><FONT size=3D2 face=3DArial><SPAN class=3D156020112-20052011>mapM =
:: (a -&gt; m=20
b) -&gt; [a] -&gt; m [b]</SPAN></FONT></DIV>
<DIV><FONT size=3D2 face=3DArial><SPAN class=3D156020112-20052011>So in =
this case: a =3D=20
Maybe Int (second arg in mapM id [Just1, Just 2, Just =
3]</SPAN></FONT><FONT=20
size=3D2 face=3DArial><SPAN class=3D156020112-20052011>&nbsp;and&nbsp;b =
=3D Int=20
and&nbsp;m =3D Maybe. So id&nbsp;is :: Maybe Int -&gt; Maybe=20
Int</SPAN></FONT></DIV>
<DIV><FONT size=3D2 face=3DArial><SPAN=20
class=3D156020112-20052011></SPAN></FONT>&nbsp;</DIV>
<DIV></SPAN></SPAN></SPAN></SPAN></FONT><FONT size=3D2 =
face=3DArial><SPAN=20
class=3D156020112-20052011>mapM id [Just 1, Nothing, Just 3]=20
</SPAN></FONT></DIV></DIV>
<DIV><FONT size=3D2 face=3DArial><SPAN =
class=3D156020112-20052011>result:=20
Nothing.</SPAN></FONT></DIV>
<DIV><FONT size=3D2 face=3DArial><SPAN class=3D156020112-20052011>My =
first guess for=20
the result: Just [Just 1, Nothing, Just 3]</SPAN></FONT></DIV>
<DIV><FONT size=3D2 face=3DArial><SPAN=20
class=3D156020112-20052011></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2 face=3DArial><SPAN class=3D156020112-20052011>when I =
do: mapM id=20
[1,2,3] I get an error (id has wrong type, which makes =
sense)</SPAN></FONT><FONT=20
size=3D2 face=3DArial><SPAN =
class=3D156020112-20052011></SPAN></FONT></DIV>
<DIV><FONT size=3D2 face=3DArial><SPAN=20
class=3D156020112-20052011></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2 face=3DArial><SPAN class=3D156020112-20052011>Can =
somebody explain=20
what is going on here?</SPAN></FONT></DIV>
<DIV><FONT size=3D2 face=3DArial><SPAN=20
class=3D156020112-20052011></SPAN></FONT>&nbsp;</DIV>
<DIV><FONT size=3D2 face=3DArial><SPAN=20
class=3D156020112-20052011>Kees</SPAN></FONT></DIV></BODY></HTML>

------=_NextPart_000_0000_01CC16FA.AB6D8BE0--




More information about the Beginners mailing list