Erlang模式匹配布尔示例

dzhpxtsq  于 2022-12-08  发布在  Erlang
关注(0)|答案(2)|浏览(164)

我想学习Erlang,书上有练习:编写一个接受逻辑表达式和布尔值的模块boolean. erl。(表示为原子true和false),并返回它们的布尔结果。您编写的函数应包括b_not/1、b_and/2、b_or/2和b_nand/2。您不应使用逻辑结构and、or和not,而是使用模式匹配来实现你的目标。2从shell测试你的模块。3在你的模块中调用导出函数的一些例子包括:

bool:b_not(false) ⇒ true
bool:b_and(false, true) ⇒ false
bool:b_and(bool:b_not(bool:b_and(true, false)), true) ⇒ true.

所以目前为止我想到的最好的解决方案是:

-module(b).

-export([b_not/1,b_and/2,b_or/2]).

b_not(false) -> false /= true.
%%%
b_and(false, false) -> false;
b_and(X, Y) -> X == Y.    
%%%
b_or(true, true) -> true;
b_or(X, Y) -> X /= Y.

如何解最后一个例子,我真的不明白。有什么帮助吗?谢谢。

rm5edbpk

rm5edbpk1#

I disagree a little with what @hdima said. The goal of this exercise is to practise pattern matching and not use any operators. The best way with pattern matching is to write down every case. So for b_or what are the different possible cases for the arguments? There are 4: true , true ; true , false ; false , true ; and false , false . So just write down each case as a separate clause returning the correct value:

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

So looking at this answer you could be thinking well as many cases are the same why not optimise this to:

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

_ is the don't care variable which matches anything and never gets bound. Well, both version behave the same if called with boolean values BUT they behave differently if called with non-boolean values. In this case the second returns true while the first one generates an exception. Usually it is better to generate an exception on bad input rather than let it pass and return a misleading answer, so the first alternative is a better solution.
Writing b_and (and b_xor ) is left to you.
Sorry if this was a bit long but pattern matching can take some getting used to. The concept of writing down what you expect and not how to test for it is different from what is common in OO and imperative languages. (I give courses and this is a common hurdle)

ehxuflar

ehxuflar2#

If you limited only to pattern matching b_not/1 should looks like this:

b_not(false) -> true;
b_not(true) -> false.

Try to use this idea for the other functions.

  • Hint: for the other functions you don't need to write clauses for every possible case.*

相关问题