{- From Learn Haskell Fast and Hard : 4.3.1. Maybe is a monad deposit value account = account + value -- You are eligible for a bonus only if your sequence of transactions stays out of the red. -- No let expressions, hence intermediate calculations are performed multiple times.
withdrawM :: (Num a,Ord a) => a -> a -> Maybe a eligibleMonad :: (Num a, Ord a) => a -> Maybe Bool main = do 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. www.dit.ie 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. www.dit.ie Tá ITBÁC ag aistriú go Gráinseach Ghormáin – DIT is on the move to Grangegorman _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
Hello,
Le mer. 28 oct. 2015 à 15:59, PATRICK BROWNE <[hidden email]> a écrit :
A priori, they're only semantically equivalent since in eligibleEquational intermediary computation are repeated whereas in eligibleLet they're only effectued once.
eligibleMonad is nice and readable while eligibleLet is a tiresome mess to write and maintain... Basically in eligibleMonad the logic that checks whether an operation is possible is rolled into the monadic operations themselves whereas in eligibleLet you have to check every operation validity yourself (so you can forget one or do it incorrectly). Note that with more familiarity with monads, you would probably rewrite eligibleMonad to avoid naming the intermediary account (and thus avoid any confusion between versions) : eligibleMonad account = depositM 100 account >>= withdrawM 200 >>= depositM 100 >>= withdrawM 300 >>= depositM 1000 >> Just True This would make it very easy to add or remove operations. -- Jedaï _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
Chaddaï, Is it true in general that let expressions (e.g. eligibleLet) are always semantically equivalent to their equational counterparts (e.g. eligibleEquational)? Would it be fair to say that "let" is syntactic sugar for an equational equivalent? Or is there more to it? With respect to the ordering of the operations is generally true that a monadic version is semantically equivalent to a set of "let expressions" in a nested if-then-else? Thanks, Pat On 3 November 2015 at 06:56, Chaddaï Fouché <[hidden email]> wrote:
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. www.dit.ie 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. www.dit.ie Tá ITBÁC ag aistriú go Gráinseach Ghormáin – DIT is on the move to Grangegorman _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
Le mar. 3 nov. 2015 à 13:24, PATRICK BROWNE <[hidden email]> a écrit :
Yes, in Haskell that is true because it is pure/referentially transparent (absent case of misuse of the "escape hatch"es like unsafePerformIO). That is one of the big advantage : that you're always able to reason equationally. (note that this remains true with monads, they're not impure, they're just a notation that can describe impure computations)
"let x = stuff in expr" is syntactic sugar for "(\x -> expr) stuff" basically. Though there's a few syntactical conveniences that complicate the translation.
Well with the Maybe monad like here yes, though that's more of a nested case-of. With IO this is more complicated, especially if you use forkIO (concurrency).
You're welcome ! -- Chaddaï _______________________________________________ Beginners mailing list [hidden email] http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners |
Free forum by Nabble | Edit this page |