2
$\begingroup$

I am very new to Mathematica, and need to know how to plug values into existing functions. For example, Math World supplies the following code for building a truth table of n levels of operator op:

TruthTable[op_, n_] := 
Module[{l = 
 Flatten[Outer[List, Sequence @@ Table[{True, False}, {n}]], 
  n - 1], a = Array[A, n]}, 
DisplayForm[
GridBox[Prepend[Append[#, op @@ #] & /@ l, Append[a, op @@ a]], 
 RowLines -> True, ColumnLines -> True]]]

How do I set the operator and the value of n?

$\endgroup$
4
  • $\begingroup$ You've seen BooleanTable[] already? $\endgroup$ Oct 27, 2012 at 7:39
  • $\begingroup$ You say "Mathematica supplies the following code for building a truth table of n levels of operator op". It it not known to my copy of Mathematica. I found it on Math World, which is not same as "supplied by Mathematica". $\endgroup$
    – m_goldberg
    Oct 27, 2012 at 7:44
  • $\begingroup$ @J.M. I have seen BooleanTable[] and the related functions and did not have problems with those--I'm only unsure about the notation used above. $\endgroup$
    – QuietThud
    Oct 27, 2012 at 7:53
  • $\begingroup$ @m_goldberg: I meant "supplied by Wolfram". $\endgroup$
    – QuietThud
    Oct 27, 2012 at 7:55

1 Answer 1

4
$\begingroup$

Here is some results of my trying out this function on my copy of Mathematica:

notebook selection

$\endgroup$
3
  • $\begingroup$ I see you've used an '@' in your definition of the op--is this a standard way of avoiding nested parentheses? Also, can you tell me about the use of the '&' at the end of the definition? $\endgroup$
    – QuietThud
    Oct 27, 2012 at 8:23
  • 2
    $\begingroup$ @Quiet, @ is a prefix method of applying a function; that is, f @ x and f[x] are the same thing. There's a postfix version, x // f. &, on the other hand, is shorthand for the Function[] construct, which allows you to use pure functions. Look at the docs for Function[] for more detail on this. $\endgroup$ Oct 27, 2012 at 8:28
  • $\begingroup$ @J.M: Thanks a lot, that's very helpful.=) $\endgroup$
    – QuietThud
    Oct 28, 2012 at 5:51

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.