4
$\begingroup$

According to a Mathematica textbook, we can write N[x,n] by using infix notation as x ~ N ~ n. How about other functions with one argument or more than 2 arguments, is it possible?

$\endgroup$

3 Answers 3

13
$\begingroup$

One can use Sequence for such purposes

Sequence[]~f~x
x~f~Sequence[]

f[x]

f[x]

x1~f~Sequence[x2, x3]

f[x1, x2, x3]

$\endgroup$
3
$\begingroup$

If, in an input cell, you type the expression,

x1 ~ f ~ x2 ~ f ~ x3

it will evaluate to

f[f[x1,x2],x3]

UNLESS the symbol f has the attribute Flat in which case it will evaluate to

f[x1,x2,x3]
$\endgroup$
0
$\begingroup$

From the Infix[] documentation page, you can:

x1 ~ f ~ x2 ~ f ~ x3 ~ ... === Infix[f[x1,x2,x3,...]]
$\endgroup$
5
  • $\begingroup$ How about functions with one argument? $\endgroup$ Dec 28, 2013 at 22:11
  • 1
    $\begingroup$ Did you actually try what you wrote above? This is wrong. Infix notation is primarily for 2 argument functions and works for multiple arguments when the function is Flat. The Infix function that you refer to is only a typesetting function. $\endgroup$
    – rm -rf
    Dec 28, 2013 at 22:13
  • $\begingroup$ @StiffJokes I'd use @. edit: @rm -rf: Is the documentation wrong? $\endgroup$
    – shrx
    Dec 28, 2013 at 22:13
  • 2
    $\begingroup$ @shrx It's confusing, especially since F1 on ~ takes you there... the Infix function is more for typesetting than the function calls notation. For instance, Prefix[f[x]] will give you f@x, but this f@x is not equivalent to typing out f@x (try evaluating the two). Check out the full forms: Infix[f[x1, x2, x3]] // FullForm and x1~f~x2~f~x3 // FullForm $\endgroup$
    – rm -rf
    Dec 28, 2013 at 22:19
  • $\begingroup$ @rm-rf Oh! I miss the times when those were hot themes here .) $\endgroup$ Dec 29, 2013 at 5:50

Your Answer

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

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