# [Haskell-cafe] Let, Equations, and Monad

PATRICK BROWNE patrick.browne at dit.ie
Tue Oct 27 12:32:35 UTC 2015

```{- From Learn Haskell Fast and Hard : 4.3.1.  Maybe is a monad
http://yannesposito.com/Scratch/en/blog/Haskell-the-Hard-Way/#maybe-monad

Concerning the code below I have the following questions:
1) Are eligibleLet and eligibleEquational operationally equivalent (i.e.
perform the same operations) and/or semantically equivalent(i.e.
Church-Rosser)?
2) Apart from the return type of Maybe instead of Bool how does
eligibleMonad differ from eligibleLet?

Regards,
Pat
-}

deposit  value account = account + value
withdraw value account = account - value

-- You are eligible for a bonus only if your sequence of transactions stays
out of the red.
eligibleLet :: (Num a,Ord a) => a -> Bool
eligibleLet account =
let account1 = deposit 100 account in
if (account1 < 0)
then False
else
let account2 = withdraw 200 account1 in
if (account2 < 0)
then False
else
let account3 = deposit 100 account2 in
if (account3 < 0)
then False
else
let account4 = withdraw 300 account3 in
if (account4 < 0)
then False
else
let account5 = deposit 1000 account4 in
if (account5 < 0)
then False
else
True

eligibleEquational :: (Num a,Ord a) => a -> Bool
eligibleEquational account =
let account1 = deposit 100 account in
if (account1 < 0)
then False
else
let account2 = withdraw 200 account1 in
if (account2 < 0)
then False
else
let account3 = deposit 100 account2 in
if (account3 < 0)
then False
else
let account4 = withdraw 300 account3 in
if (account4 < 0)
then False
else
let account5 = deposit 1000 account4 in
if (account5 < 0)
then False
else
True

-- Monadic version
depositM :: (Num a) => a -> a -> Maybe a
depositM value account = Just (account + value)

withdrawM :: (Num a,Ord a) => a -> a -> Maybe a
withdrawM value account = if (account < value)
then Nothing
else Just (account - value)

eligibleMonad :: (Num a, Ord a) => a -> Maybe Bool
eligibleMonad account = do
account1 <- depositM 100 account
account2 <- withdrawM 200 account1
account3 <- depositM 100 account2
account4 <- withdrawM 300 account3
account5 <- depositM 1000 account4
Just True

main = do
print \$ eligibleLet 300 -- True
print \$ eligibleLet 299 -- Falsen
print \$ eligibleEquational 300 -- True
print \$ eligibleEquational 299 -- False
print \$ eligibleMonad 300 -- Just True
print \$ eligibleMonad 299 -- Nothing

--

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 <http://www.dit.ie/grangegorman>
-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.haskell.org/pipermail/haskell-cafe/attachments/20151027/59a6857b/attachment.html>
```

More information about the Haskell-Cafe mailing list