How do I write a function that only takes binary input?
Your question is not very clear, but I guess something like this meets your requirements:
Another way to skin this cat is to define your own test to verify what you consider binary. For example, if you mean a single binary digit you would use
Or some other analogous expression. This form can lead to a cleaner implementation when you have more than one variable
You can also use patterns to define more general combinations of binary values in the arguments of your functions. Moreover, by changing the implementation of BinaryQ you can accomodate more elaborate definitions of what is binary. For example, you want True-False as in logic prepositions? Then you will be better off with
(Obviously, in this case you should change the implementation of logicFun accordingly). Is your definition of binary is a list of 0s and 1s? Then
will do the trick:
(the definition of logicFun here is just for illustrative purposes). I guess nothing prevents you from writing a more complex BinaryQ function that could combine single digit binary cases with lists of binary digits or even wrapper that characterize binary input.