# Full programs, functions, and snippets in Mathematica

This answer prompted a question that I've wondered for a while so I could answer New users' guides to golfing rules in specific languages, which is what exactly constitutes a full program, function, and snippet in Mathematica (more accurately, the Wolfram Language).

In Mathematica, everything is an expression. There are atomic expressions like numbers, strings, images, etc., and there are compound expressions (not to be confused with CompoundExpression) of the form f[x, y, ...], where f, x, y, etc. are arbitrary expressions. For example consider the following program:

x = Input[];
x++;
Print[x]


This is just syntactic sugar for the following expression:

CompoundExpression[Set[x, Input[]], Increment[x], Print[x]]


In fact, the following would evaluate to True:

Hold[x = Input[];
x++;
Print[x]] == Hold[CompoundExpression[Set[x, Input[]], Increment[x], Print[x]]]


All it means to run a Mathematica program is to evaluate it as an expression, and I guess you could say that the return value is whatever the expression evaluates to (not to be confused with Return). Now any Mathematica expression could be used as the head of a compound expression, even the above program. So how does this relate to our definitions of full program or function?

The accepted answer to Full program in Clojure says that "A full program is anything that can be compiled/interpreted on its own, usually from a standalone file, using any pre-existing compiler/interpreter." Every (syntactically valid) Mathematica expression satisfies this (there is no boiler plate, just save the expression to a .wl file an Get it in Mathematica or run it from command line). Conversely, everything we would call a Mathematica program is an expression, so it seems full program = expression.

The accepted answer to What even is a “function” by our standards? says that a function "is a section of code, which can be directly inserted into a program with no modification, and should be able to be assigned/named/referred to in some way. It should be able to run independent of surrounding code. It should be able to take input/output in some form." Again, any Mathematica expression fits the bill. For example, we could do the following:

f := (x = Input[]; x++; Print[x])


Then we could pass in arguments as f[x, y, ...]. In this example, the arguments would be ignored and the user would be prompted for input when the evaluator got to Input[], but that's not the point. There is absolutely no difference between which pieces of Mathematica code can be named, used as a function, passed as an argument, interpreted as a standalone program, etc. And this isn't just a quirk of the language, this is an essential part of the Mathematica paradigm.

Finally, what even is a snippet? I see references to "REPL snippets" (e.g. here), but the only formal definition I've seen is this answer, which seems more specific than how the term is commonly used. For example, the question that led to this post was whether $SystemWordLength was a valid submission or if it was just a REPL snippet, which it doesn't seem to be. So what exactly do these terms mean with respect to Mathematica? ## 1 Answer It seems to me that for Mathematica (and potentially other languages), the function/program distinction for any given code isn't intrinsic to the code, but depends on how you use the code to satisfy a question's spec. If evaluating code on its own gives the correct output (asking the user for input if appropriate), then we call it a full program solution (code could be used as the head of an expression, but it most likely wouldn't satisfy the spec). Likewise, if you must evaluate code[x, y, ...] where x, y, ..., are the input in order to get the correct output, then we call it a function solution (you could evaluate code on its own without using it as the head of an expression, but it most likely wouldn't satisfy the spec). I would say that $SystemWordLength is a perfectly valid full program solution to Is my OS 32-bit or 64-bit?. It's not a snippet since it doesn't expect input to be hardcoded into some variable. Note that it doesn't matter whether we consider it as Mathematica REPL code or a .wl file run from the command line. In the former case, the evaluated expression is printed implicitly, and in the latter, programs are allowed to output using their return value.

• I think that a "full program" should be something that can be invoked (and used) from the command line, not something that can be imported in some other program of the same language. The "return value" you're talking about is not accessible when invoking a file containing $SystemWordLength as a script from the command line. – Martin Ender Jun 22 '17 at 18:25 • @MartinEnder So it seems that you're right that if you do wolframscript -file code.wl, it won't output anything unless you use something like Print, and there doesn't seem to be any way to get the returned expression. However, you can do wolframscript -code$SystemWordLength and it will output the result. Also, since we define languages by their implementations, this is only a drawback of wolframscript, not Mathematica. You can run a Mathematica program by entering the code into a notebook, using Get, or opening the .wl file and clicking Run All Code, all with the same result. – ngenisis Jun 23 '17 at 0:07