[Haskell-beginners] How to avoid evaluating the second (undefined) argument of a Boolean AND operation?

Jack Henahan jhenahan at uvm.edu
Thu Jun 23 01:07:45 CEST 2011


Use

    myAND F _ = F

instead of using x.

On Jun 22, 2011, at 7:03 PM, Costello, Roger L. wrote:

> Hi Folks,
> 
> Here is my own version of the Bool datatype, and my own version of the Boolean AND:
> 
> data MyBool = F | T
> 
> myAND :: MyBool -> MyBool -> MyBool
> myAND F x = F
> myAND T x = x
> 
> If the first argument is F then return F. I assumed that the second argument would not even bother being evaluated.
> 
> I figured that I could provide an undefined value for the second argument:
> 
>     myAND F (1 / 0) 
> 
> However, that doesn't work. I get this error message:
> 
>    No instance for (Fractional MyBool)
>      arising from a use of `/' 
>    Possible fix: add an instance declaration for (Fractional MyBool)
>    In the second argument of `myAND', namely `(1 / 0)'
>    In the expression: myAND F (1 / 0)
>    In the definition of `t4': t4 = myAND F (1 / 0)
> 
> Why does it evaluate the second argument when the answer is already known from the first argument? 
> 
> How can I design it so that if the answer is known from the first argument, then an undefined second argument doesn't produce an error?
> 
> /Roger
> 
> _______________________________________________
> Beginners mailing list
> Beginners at haskell.org
> http://www.haskell.org/mailman/listinfo/beginners

-------------- next part --------------
A non-text attachment was scrubbed...
Name: 398E692F.asc
Type: application/x-apple-msg-attachment
Size: 24295 bytes
Desc: not available
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110622/35b94187/attachment-0001.bin>
-------------- next part --------------


-------------- next part --------------
A non-text attachment was scrubbed...
Name: PGP.sig
Type: application/pgp-signature
Size: 881 bytes
Desc: This is a digitally signed message part
URL: <http://www.haskell.org/pipermail/beginners/attachments/20110622/35b94187/attachment-0001.pgp>


More information about the Beginners mailing list