<meta http-equiv="content-type" content="text/html; charset=utf-8"><br><div class="gmail_quote">On Wed, Jul 7, 2010 at 8:48 PM, John Meacham <span dir="ltr"><<a href="mailto:john@repetae.net">john@repetae.net</a>></span> wrote:<br>
<blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;">Are you sure you are interpreting what 'die' should do properly? Your<br>
code makes sense if die should decrement your life counter and continue<br>
along, however if 'die' is meant to end your whole game, then there is<br>
another implementation that does type check.<br>
<br>
John<br></blockquote><div> </div><div>You're absolutely right, I sen't the wrong code, here's the "correct" one and a little bit more explanation about what checkpoint does. </div><div><br>
</div><div><div>The result of die makes sense for the checkPoint function since there are three cases for it:<br>1) The player died and has no remaining lifes. The game can't continue, I just return Noting in the die function and in checkpoint make the corresponding case.</div>
<div>2) The player died and has remaining lifes. The game can be retried with a life subtracted. I would need to tell checkpoint that I died and I want to retry, that's where I think the result is important, because of the next case.</div>
</div><div>3) The player didn't died, it finished the particular game and checkpoint m equals m. Here I would need to see if the result of the game was different from the result from die, and continue.</div><div><br>
</div>
<div><div>instance GameMonad Game where</div><div> extraLife = Game $ \l -> Just ((),l+1)</div><div> getLives = Game $ \l -> Just (l,l)</div><div> die = do</div><div> n <- getLives</div><div>
if n <= 0 then Game $ \_ -> Nothing</div><div> else Game $ \_ -> Just ("player died",n-1)</div><div> checkPoint a = do</div><div> n <- getLives</div><div> case execGame a n of</div><div>
Nothing -> Game $ \_ -> Nothing</div><div> Just c -> gameOn $ fst c</div><div> where gameOn "player died" = a >>= \_ -> (checkPoint a)</div><div> gameOn _ = a</div>
</div><div> </div><div>Obviously this fails to compile because I'm returning a String and it doesn't match with a either, but the idea of what I think I need to do is right there.</div><div><br></div><div><span class="Apple-style-span" style="font-size: 13px; border-collapse: collapse; -webkit-border-horizontal-spacing: 2px; -webkit-border-vertical-spacing: 2px; "><font class="Apple-style-span" face="arial, helvetica, sans-serif">Ivan Miljenovic told</font></span> me to use error, and actually I though something like that. in STM retry combined with atomically does something similar as what I need checkpoint and die to do, and they use exceptions to accomplish it. I really think that's the solution I want, but then I have another question, when I 'throw' the exception in die and 'catch' it in checkpoint to call it again, is the number of lives gonna be lives - 1?</div>
<div><br></div><div>Thanks for answering so quickly,</div><div><br></div><div>Hector Guilarte</div><div><br></div><div>Pd: Here's an example run of how my homework should work after is finished</div><div><br></div><div>
<div>printLives :: ( GameMonad m , MonadIO m ) = > String -> m ()</div><div>printLives = do</div><div> n <- getLives</div><div> liftIO $ putStrLn $ s ++ " " ++ show n</div><div>test1 :: ( GameMonad m , MonadIO m ) = > m ()</div>
<div>test1 = checkPoint $ do</div><div> printLives " Vidas : "</div><div> die</div><div> liftIO $ putStrLn " Ganamos ! "</div></div><div><br></div><div><div>lastChance :: GameMonad m = > m ()</div>
<div>lastChance = do</div><div> n <- getLives</div><div> if n == 1 then return ()</div><div> else die</div><div>test2 :: ( GameMonad m , MonadIO m ) = > m String</div><div>test2 = checkPoint $ do</div>
<div> printLives " Inicio "</div><div> n <- getLives</div><div> if n == 1</div><div> then do</div><div> liftIO $ putStrLn " Final "</div><div> return " Victoria ! "</div><div>
else do</div><div> checkPoint $ do</div><div> printLives " Checkpoint anidado "</div><div> lastChance</div><div> extraLife</div><div> printLives " Vida extra ! "</div>
<div>
die</div></div><div><br></div><div>AND THE OUTPUT TO SOME CALLS</div><div><br></div><div><div>ghci > runGameT test1 3</div><div>Vidas : 3</div><div>Vidas : 2</div><div>Vidas : 1</div><div>Nothing</div><div>ghci > runGameT test2 3</div>
<div>Inicio 3</div><div>Checkpoint anidado 3</div><div>Checkpoint anidado 2</div><div>Checkpoint anidado 1</div><div>Vida extra ! 2</div><div>Inicio 1</div><div>Finish</div><div>Just ( " Victoria ! " ,1)</div></div>
<div><br></div><blockquote class="gmail_quote" style="margin:0 0 0 .8ex;border-left:1px #ccc solid;padding-left:1ex;"><font color="#888888">
--<br>
John Meacham - ⑆<a href="http://repetae.net" target="_blank">repetae.net</a>⑆john⑈ - <a href="http://notanumber.net/" target="_blank">http://notanumber.net/</a><br>
_______________________________________________<br>
Haskell-Cafe mailing list<br>
<a href="mailto:Haskell-Cafe@haskell.org">Haskell-Cafe@haskell.org</a><br>
<a href="http://www.haskell.org/mailman/listinfo/haskell-cafe" target="_blank">http://www.haskell.org/mailman/listinfo/haskell-cafe</a><br>
</font></blockquote></div><br>