2
$\begingroup$

Consider this example:

 Cases[{4, x, 5 a , 6 x}, x_.*a_ :> x]

(*{1, 1, 5, 6}*)

It is clear that the _. took 1 as a Default value.

Now:

f[x_.] := {x} dose not work and _. Default value has to be user defined first

Default[f] = 0;
f[x_.] := {x}
f[]
(*{0}*)

It is little confusing that _. takes default value in some operation and has to be user defined with default value in other operation.

Any explanations?

Thanks

$\endgroup$
3
  • 2
    $\begingroup$ The functions Times, Plus, Power have built-in default values (1, 0, and 1, respectively). $\endgroup$
    – kglr
    Commented Dec 29, 2014 at 0:56
  • 2
    $\begingroup$ Use DefaultValues[Times], DefaultValues[Plus] etc. to see this. Now try DefaultValues[f] before you have attached any default values to it, and you will see that f has no built-in default values. $\endgroup$
    – C. E.
    Commented Dec 29, 2014 at 0:57
  • $\begingroup$ Ok. Thanks. It is clear now:) $\endgroup$ Commented Dec 29, 2014 at 1:05

1 Answer 1

2
$\begingroup$

The answer is pretty simple and you were very close to seeing this yourself:

Default[Times]

(* Out[9]= 1 *)

As you see, the DefaultValues for Times and Plus are already built in.

$\endgroup$
1
  • $\begingroup$ I was very close yes but I would never seeing it myself before your answer. Thanks :) $\endgroup$ Commented Dec 29, 2014 at 1:03

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.