<div dir="ltr"><p>{- From Learn Haskell Fast and Hard : 4.3.1. Maybe is a monad <br> <a href="http://yannesposito.com/Scratch/en/blog/Haskell-the-Hard-Way/#maybe-monad">http://yannesposito.com/Scratch/en/blog/Haskell-the-Hard-Way/#maybe-monad</a><br> <br> Concerning the code below I have the following questions:<br> 1) Are eligibleLet and eligibleEquational operationally equivalent (i.e. perform the same operations) and/or semantically equivalent(i.e. Church-Rosser)?<br> 2) Apart from the return type of Maybe instead of Bool how does eligibleMonad differ from eligibleLet?<br> <br> Regards,<br> Pat<br> -}</p><p>deposit value account = account + value<br>withdraw value account = account - value</p><p>-- You are eligible for a bonus only if your sequence of transactions stays out of the red.<br>eligibleLet :: (Num a,Ord a) => a -> Bool<br>eligibleLet account =<br> let account1 = deposit 100 account in<br> if (account1 < 0)<br> then False<br> else<br> let account2 = withdraw 200 account1 in<br> if (account2 < 0)<br> then False<br> else<br> let account3 = deposit 100 account2 in<br> if (account3 < 0)<br> then False<br> else<br> let account4 = withdraw 300 account3 in<br> if (account4 < 0)<br> then False<br> else<br> let account5 = deposit 1000 account4 in<br> if (account5 < 0)<br> then False<br> else<br> True</p><p>-- No let expressions, hence intermediate calculations are performed multiple times.<br>-- Should terminates on first point of failure <br>eligibleEquational :: (Num a,Ord a) => a -> Bool<br>eligibleEquational account = if (deposit 100 account) < 0 then False else<br> if (withdraw 200 (deposit 100 account)) < 0 then False else<br> if (deposit 100 (withdraw 200 (deposit 100 account))) < 0 then False else<br> if (withdraw 300 (deposit 100 (withdraw 200 (deposit 100 account)))) < 0 then False else<br> if (deposit 1000 (withdraw 300 (deposit 100 (withdraw 200 (deposit 100 account))))) < 0 then False else<br> True<br> </p><p><br>-- Monadic version <br>depositM :: (Num a) => a -> a -> Maybe a<br>depositM value account = Just (account + value)</p><p>withdrawM :: (Num a,Ord a) => a -> a -> Maybe a<br>withdrawM value account = if (account < value) <br> then Nothing <br> else Just (account - value)</p><p>eligibleMonad :: (Num a, Ord a) => a -> Maybe Bool<br>eligibleMonad account = do<br> account1 <- depositM 100 account <br> account2 <- withdrawM 200 account1 <br> account3 <- depositM 100 account2 <br> account4 <- withdrawM 300 account3 <br> account5 <- depositM 1000 account4<br> Just True</p><p>main = do<br> print $ eligibleLet 300 -- True<br> print $ eligibleLet 299 -- Falsen<br> print $ eligibleEquational 300 -- True<br> print $ eligibleEquational 299 -- False<br> print $ eligibleMonad 300 -- Just True<br> print $ eligibleMonad 299 -- Nothing<br> <br> </p></div>
<br>
<p><span lang="EN-GB"><font size="2">This email originated from DIT. If you received this email in error, please delete it from your system. Please note that if you are not the named addressee, disclosing, copying, distributing or taking any action based on the contents of this email or attachments is prohibited. <a href="http://www.dit.ie/" target="_blank">www.dit.ie</a></font></span></p><p><font size="2">Is ó ITBÁC
a tháinig an ríomhphost seo. Má fuair tú an ríomhphost seo trí earráid, scrios
de do chóras é le do thoil. Tabhair ar aird, mura tú an seolaí ainmnithe, go
bhfuil dianchosc ar aon nochtadh, aon chóipeáil, aon dáileadh nó ar aon ghníomh
a dhéanfar bunaithe ar an ábhar atá sa ríomhphost nó sna hiatáin seo. <a href="http://www.dit.ie/" target="_blank">www.dit.ie</a></font></p><p><a href="http://www.dit.ie/grangegorman" target="_blank"><font size="2">Tá ITBÁC ag aistriú go Gráinseach Ghormáin – DIT is on the move to Grangegorman</font></a></p>