Group Abstract Group Abstract

Message Boards Message Boards

0
|
3.5K Views
|
2 Replies
|
1 Total Like
View groups...
Share
Share this post:

Weird 3-blanks argument in defining function

Posted 3 years ago

Consider defining such a function with blanks arguments

f\[x BlankBlank,  y BlankBlank, z BlankBlankBlank\] := x + y - z

not that the third arguments with 3 blanks ("x" or "y" follows two blanks). When input three actual parameters, the function is normal. But when input 4 actual parameters, the results are stranger: when z serves as an empty, it seems obtain a value -1!

Input\[1\]: f\[100, 1, 2, 3\],
Output\[3\]: 95.

To know the details, I get all the possible permutation distributing the actual parameters

Input\[2\]:ReplaceList
Output\[2\]:{95, 100, 105, 100, 105, 105}

which, for easy seeing the detail, corresponds to the following:

Input\[3\]ReplaceList\[f\[100, 1, 2, 3\], 
  f-> f\[x\] + f\[y\] - f\[z\]\] // TableForm;

Output[3] shows that when f[z] serves as f[], it get 1. but if the function was revised as x+y+z, when f[z] serves as f[], it would get 0. Why do -f[] get 1 and f[] get 0? See the program.nb attached.

Attachments:
POSTED BY: Math Logic
2 Replies
Posted 3 years ago

Oh! Got it! It was not weird, for the answer is wonderful!

POSTED BY: Math Logic
Posted 3 years ago

A better way to "know the details" is to use Trace. But let's start from the beginning...

Consider your original definition:

f[x__, y__, z___] := x + y - z

Unless there is some weird example I haven't thought of, this is effectively equivalent to

f[x_, y_, z___] := x + y - z

So, I'm not sure why you're using BlankSequence instead of just Blank. Not really important, but maybe there is some confusion about basic pattern matching.

Okay, so let's break down this expression:

f[100, 1, 2, 3]

We get (before the evaluation is totally complete)

Plus[100, 1, Times[-1, 2, 3]]

Why? Well, just look at this:

x + y - z // FullForm
(*Plus[x, y, Times[-1, z]]*)

So, our variable z was assigned the value Sequence[2,3], and that sequence became part of a Times expression, and you can work out the arithmetic from there.

Now, about your question about why f[] gives 0 and -f[] gives 1. Hopefully you see now that this isn't actually what's happening. Furthermore, looking at all possible ways to assign x, y, and z (which is what ReplaceList is doing) isn't actually relevant. As I said at the outset, there is no effective difference in your particular pattern sequence between using BlankSequence or just Blank. So, when applying f, you're never going to see any of these other ways to assign x, y, and z.

When you encounter an output that surprises you, one thing that is often helpful is to use Trace.

f[100, 1, 2, 3] // Trace
(*{f[100,1,2,3],100+1-2 3,{-2 3,-6},100+1-6,95}*)

Also, FullForm can be helpful.

f[a, b, c, d, e, f] // FullForm
(*Plus[a,b,Times[-1,c,d,e,f]]*)
POSTED BY: Eric Rimbey
Reply to this discussion
Community posts can be styled and formatted using the Markdown syntax.
Reply Preview
Attachments
Remove
or Discard