Send Beginners mailing list submissions to
        beginners@haskell.org

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
        beginners-requ...@haskell.org

You can reach the person managing the list at
        beginners-ow...@haskell.org

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


Today's Topics:

   1.  Conditionals where more than one case is true (PATRICK BROWNE)
   2. Re:  Conditionals where more than one case is     true
      (Francesco Ariis)


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

Message: 1
Date: Mon, 17 Apr 2017 11:26:07 +0100
From: PATRICK BROWNE <patrick.bro...@dit.ie>
To: The Haskell-Beginners Mailing List - Discussion of primarily
        beginner-level topics related to Haskell <beginners@haskell.org>
Subject: [Haskell-beginners] Conditionals where more than one case is
        true
Message-ID:
        <CAGFLrKcBV0yS3PimtEGzMYPG=Mdu=7z272vgc9mjnd4edhr...@mail.gmail.com>
Content-Type: text/plain; charset="utf-8"

I am having difficulty in evaluating conditionals where more than one case
is true.
I require a program to express the following conditions:
 1) x is faster than y if x is a buffolo and y is a pig.
 2) x is faster than y if x is a pig and y is a slug.
Both of these conditions are true (expressed in fact predicate).
I include my attempt using guards (I have also tried case/if-then-else)

I can apreciate that the program works when the first guard condition is
met (faster Bob Joe).
I am not sure why it fails on the second case (faster Bob Joe)

Can I program the requirement using conditionals?
Can I pattern match is some way?
Is there another way to encode the requirement.
Regards,
Pat


data E = Bob | Joe | Steve | Buffalo | Pig | Slug  deriving Show

fact Buffalo Bob  = True
fact Pig Joe  = True
fact Slug Steve  = True


faster x y | fact Buffalo x && fact Pig y =  True
           | fact Pig x && fact Slug y =  True
           | otherwise = False
-- faster Bob Joe OK
-- faster Steve  Bob ***Exception: Faster.hs:(5,1)-(7,23): Non-exhaustive
patterns in function fact

-- 


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/20170417/d68e1e57/attachment-0001.html>

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

Message: 2
Date: Mon, 17 Apr 2017 12:54:12 +0200
From: Francesco Ariis <fa...@ariis.it>
To: beginners@haskell.org
Subject: Re: [Haskell-beginners] Conditionals where more than one case
        is      true
Message-ID: <20170417105412.ga14...@casa.casa>
Content-Type: text/plain; charset=us-ascii

On Mon, Apr 17, 2017 at 11:26:07AM +0100, PATRICK BROWNE wrote:
> I can apreciate that the program works when the first guard condition is
> met (faster Bob Joe).
> I am not sure why it fails on the second case (faster Bob Joe)

Your `fact` function is the one that is partial

    fact Buffalo Bob = True
    fact Pig Joe     = True
    fact Slug Steve  = True
    -- what if all those patterns fail?

You should add a line to handle "every other case", like

    fact Buffalo Bob = True
    fact Pig Joe     = True
    fact Slug Steve  = True
    fact _    _      = False

Does that help?


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

Subject: Digest Footer

_______________________________________________
Beginners mailing list
Beginners@haskell.org
http://mail.haskell.org/cgi-bin/mailman/listinfo/beginners


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

End of Beginners Digest, Vol 106, Issue 9
*****************************************

Reply via email to