[Haskell-cafe] Let, Equations, and Monad

Nicola Gigante nicola.gigante at gmail.com
Tue Oct 27 12:58:42 UTC 2015


> Il giorno 27 ott 2015, alle ore 13:32, PATRICK BROWNE <patrick.browne at dit.ie> ha scritto:
> 
> {- 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
> 

Hi, I may not be able to correctly answer your question, 
but I’d make you notice that you don’t need all that nesting to
express the same thing. Since those computations are pure, 
you don’t need to sequence them in that way:

eligible account = all (< 0) [account1, account2, account3, account4]
  where
    account1 = deposit 100 account
    account2 = withdraw 200 account1
    account3 = deposit 100 account2
    account4 = withdraw 300 account3
    account5 = deposit 1000 account4

Greetings,
Nicola




More information about the Haskell-Cafe mailing list