[erlang-questions] nand problem
Richard A. O'Keefe
ok@REDACTED
Fri Jan 30 06:22:23 CET 2015
On 27/01/2015, at 6:36 am, Roelof Wobben <r.wobben@REDACTED> wrote:
> 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.
It all depends.
In languages like SML, Haskell, and F#, the type system ensures that the
arguments *cannot* be anything other than the declared type. So
b_nand :: Bool -> Bool -> Bool
b_nand True True = False
b_nand _ _ = True
is as safe as it could possibly be.
In languages like Prolog and Erlang, the absence of a static type system
means that the arguments could be anything, so writing
nand(true, true, false).
nand(_, _, true).
in Prolog would allow the query nand(1, 1, X) to succeed with X = true.
However, suppose you have a data type with N possible values.
Think of the abstract syntax trees that the Erlang compiler uses,
where N is in the low dozens.
Now suppose you want to write a function that takes two of these
values, but only has O(N) cases that make sense. You *could* write
out all N**2 combinations, but you would have to be mad.
It's only when N is small (like in your example where N = 2) that
avoiding _ begins to make sense.
More information about the erlang-questions
mailing list