[erlang-questions] nand problem

Hugo Mills <>
Mon Jan 26 18:50:05 CET 2015


On Mon, Jan 26, 2015 at 06:40:56PM +0100, Roelof Wobben wrote:
> Roelof Wobben schreef op 26-1-2015 om 18:36:
> >Hugo Mills schreef op 26-1-2015 om 18:26:
> >>On Mon, Jan 26, 2015 at 05:21:42PM +0100, Roelof Wobben wrote:
> >>[snip]
> >>>b_and(true, true) ->
> >>>   true;
> >>>
> >>>b_and(true, false) ->
> >>>   false;
> >>>
> >>>b_and(false, true) ->
> >>>   false;
> >>>
> >>>b_and(false, false) ->
> >>>   false.
> >>    You could make this shorter and possibly easier to read with
> >>
> >>b_and(true, true) ->
> >>     true;
> >>b_and(_, _) ->
> >>     false.
> >>
> >>    i.e. define the (one) special case, and then just say that
> >>everything else evaluates to false. You can do something similar with
> >>b_or.
> >>
> >>    Hugo.
> >>
> >
> >Yes, I could do that .
> >I thought I have read somewhere that using _ for defensive
> >programming was nog good practice
> >but that was on using other on case on the next chapter.
> >
> >I will change my code.
> >
> >Roelof
> >
> >
> >
> >_______________________________________________
> >erlang-questions mailing list
> >
> >http://erlang.org/mailman/listinfo/erlang-questions
> >
> 
> This one better :
> 
> -module(boolean).
> 
> -export([b_not/1, b_and/2, b_or/2, b_nand/2]).
> 
> b_not(true) ->
>   false;
> 
> b_not(_) ->
>   true.
> 
> b_and(true, true) ->
>   true;
> 
> b_and(_, _) ->
>   false.
> 
> 
> b_or(true, _) ->
>   true;
> 
> b_or(_, true) ->
>   true;
> 
> b_or(false, false) ->
>   false.

   Or just:

b_or(false, false) ->
   false;
b_or(_, _) ->
   true.

> b_nand(true,true) ->
>     b_not(b_and(true, true));
> 
> b_nand(_, _) ->
>   b_not(b_and(true, false));

   You don't need different clauses here. NAND is defined as the NOT
of the AND of its two arguments. Or, in other words:

b_nand(X, Y) ->
    b_not(b_and(X, Y)).

just as Ivan suggested.

   Hugo.

-- 
Hugo Mills             | Two things came out of Berkeley in the 1960s: LSD
 carfax.org.uk | and Unix. This is not a coincidence.
http://carfax.org.uk/  |
PGP: 65E74AC0          |
-------------- next part --------------
A non-text attachment was scrubbed...
Name: signature.asc
Type: application/pgp-signature
Size: 836 bytes
Desc: Digital signature
URL: <http://erlang.org/pipermail/erlang-questions/attachments/20150126/301febce/attachment.bin>


More information about the erlang-questions mailing list