Send Beginners mailing list submissions to
        [email protected]

To subscribe or unsubscribe via the World Wide Web, visit
        http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners
or, via email, send a message with subject or body 'help' to
        [email protected]

You can reach the person managing the list at
        [email protected]

When replying, please edit your Subject line so it is more specific
than "Re: Contents of Beginners digest..."


Today's Topics:

   1.  let , equations, and monads (PATRICK BROWNE)


----------------------------------------------------------------------

Message: 1
Date: Wed, 28 Oct 2015 14:59:09 +0000
From: PATRICK BROWNE <[email protected]>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <[email protected]>
Subject: [Haskell-beginners] let , equations, and monads
Message-ID:
        <cagflrkf6t1cbultwhhktgunceq+-9d5txp5p8osz4m8hedg...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

{- 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

-- No let expressions, hence intermediate calculations are performed
multiple times.
-- Should terminates on first point of failure
eligibleEquational :: (Num a,Ord a) => a -> Bool
eligibleEquational account =  if (deposit 100 account) < 0 then False else
                              if (withdraw 200 (deposit 100 account)) < 0
then False  else
                              if (deposit 100 (withdraw 200 (deposit 100
account))) < 0  then False else
                              if (withdraw 300 (deposit 100 (withdraw 200
(deposit 100 account)))) < 0 then False else
                              if (deposit 1000 (withdraw 300 (deposit 100
(withdraw 200 (deposit 100 account))))) < 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/beginners/attachments/20151028/d2161198/attachment-0001.html>

------------------------------

Subject: Digest Footer

_______________________________________________
Beginners mailing list
[email protected]
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


------------------------------

End of Beginners Digest, Vol 88, Issue 27
*****************************************

Reply via email to