1
$\begingroup$

Consider the following snippet,

fib[1] = fib[2] = 1;
fib[n_Integer] := fib[n - 1] + fib[n - 2]

As a newbie, it seems for me, the first line assigns 1 to fib[1] and fib[2] whereas the fib has not been defined yet.

Question

How can we do that? What is the philosophy behind it (from Mathematica Language's point of view)?

$\endgroup$
5
  • $\begingroup$ This question might be too simple, but confusing for me! $\endgroup$ Commented Jul 22, 2017 at 20:06
  • 1
    $\begingroup$ Mathematica is a system of symbols that does not need to be "defined" (whatever you mean by "defining a symbol"). If a symbol x is new, there is nothing to do with it, so it simply stays inert, that is to say If you ask to evaluate x the answer is x. $\endgroup$
    – andre314
    Commented Jul 22, 2017 at 20:42
  • $\begingroup$ See mathprogramming-intro.org/book/node17.html for a discussion of rewrite rules, downvalues, and what functions really are in Mathematica. $\endgroup$
    – Michael E2
    Commented Jul 22, 2017 at 21:46
  • $\begingroup$ The complete definition of the recursion is fib[1] = fib[2] = 1; fib[n_Integer?Positive] := fib[n] = fib[n - 1] + fib[n - 2]; fib[n_Integer?NonPositive] := fib[n] = fib[n + 2] - fib[n + 1]; Check with And @@ (Fibonacci[#] == fib[#] & /@ Range[-25, 25]) $\endgroup$
    – Bob Hanlon
    Commented Jul 22, 2017 at 22:45
  • $\begingroup$ Even in just mathematics, fib[1] = fib[2] = 1 is a definition of the function fib. It just defines the specific values of the function at the specific point, 1 and 2, although it doesn't cover all domain of the function. $\endgroup$ Commented Jul 23, 2017 at 21:10

2 Answers 2

4
$\begingroup$

You appear to have expectations that do not conform to the structure of Mathematica definitions and assignments. As Alan notes in his answer f[1] = value is already a definition, specifically a DownValues rule attached to the Symbol f. Such definitions are common in Mathematica, and at least in some contexts are known as indexed objects:

Many definitions attached to the same Symbol can coexist in Mathematica, and their order of application is determined by certain rules including pattern specificity. Consider for example:

f[a_] = 3;
f[1] = 5;

f[1]
5

This happens because 1 is considered more specific than _, and this definition is placed higher in the rules list despite being specificed second.

?f

Global`f

f[1]=5

f[a_]=3
$\endgroup$
5
$\begingroup$

In fact, you did already define the "function", but only for certain values.

ClearAll[fib]
DownValues[fib]  (* no down values *)
fib[1]  (* no downvalues, so no substitution *)
(* add down values: *)
fib[1] = fib[2] = 1;  (* add down values *)
DownValues[fib]  (* two down values *)
fib[1]  (* matches a down value, so substitution *)
fib[3]  (* does not match, so no substitution *)
fib[n_Integer] := 
 fib[n - 1] + fib[n - 2]  (* add another down value *)
DownValues[fib]  (* three down values *)
fib[3] (* now this matches, so substitution *)
$\endgroup$

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.