
The Wolfram Language: Fast Introduction for Programmers - nkurz
http://www.wolfram.com/language/fast-introduction-for-programmers/
======
lindbergh
Programming in Mathematica is weird. I've done a whole course in machine
learning using only mathematica (as opposed to more obvious choices, like
matlab or python) and while writing small functions was very easy (and
functional programming fun), compounding those functions without using global
variables was very hard. I kept using the Module construct where each local
variable has to be declared, and it ended up being a mess. I always have a
running session of Mathematica running for quick calculation or plotting
(easier imo than matlab+nupy / matlab), but I would never consider it for
larger projects. But I've never seen a larger codebase so maybe there's some
kind of design pattern essential for this language.

~~~
taliesinb
Module (and its dynamical scoping cousin Block) can be clunky to use because
you have to declare the local variables you wish to use up-front. Other than
that it's not 'weird'.

I write a lot of Mathematica code for my job. To make life easier I've built
in a macro system that makes day-to-day programming a whole lot more pleasant.
It will be available in Mathematica 10.4 in the GeneralUtilities package that
ships with Mathematica.

One of the built-in macros is a construct that automatically localizes any
variables that occur as the L-value of an assignment:

    
    
      Needs["GeneralUtilities`"]
      foo[x_, y_] := Scope[
        z = x * y;
        PrimeQ[z + 1]
      ]
    

This gets rewritten at definition time to be:

    
    
      foo[x_, y_] := Block[{z},
        z = x * y;
        PrimeQ[z + 1]
      ]
    

It's a small thing, but it adds up, especially for large functions, and indeed
its silly this isn't a core feature of the language.

There are a couple other nice features of Scope, too, like the ability to use
a single quote ' anywhere in the body of your function to Echo the value there
to the current notebook without actually changing the behavior of the code --
essentially a very lightweight form of 'debugging print'. That also makes a
huge difference.

And it has some more powerful variants, like "z '= 2 * 3" will echo a line
that explicitly says "z = 6", rather than just "6".

Likewise '' means EchoHold, which prints the expression before being evaluated
AND afterwards, so you'll see "z = 2 * 3 = 6" in the above case.

Lastly ''' means Tap, which is a higher-order form of EchoHold which
instruments a function to echo its input and output (again without changing
behavior). So Fold[Plus''', Range[10]] prints out all the intermediate steps
in the fold.

There's a variety of features that add a very lightweight syntax for monadic
and exception-based error handling, but that's not quite stable enough to
advertise yet.

Same goes for debugging: there's some really powerful stack browsing, history
capture and replay features, but again not quite stable enough to advertise
for this version.

------
tunesmith
I guess the mental block I have with Wolfram/Mathematica is that there's this
feeling that you don't know how to do it until you know how to do it. As
opposed to languages where you can learn a smaller number of principles and
then use those principles to reason your way to how to do it.

Like, the third slide of the introduction:

Range[20]

Why not just something like List[1..20] ? Maybe List[1..20] works in Wolfram,
but then it's weird that you have this Range function that serves no other
purpose than to define a Range.

Same with NestList[f, x, 5]. I don't offhand know the way to create its output
in something like Scala or Haskell but I imagine it's pretty concise, but
meanwhile, in this case... if it's possible to get the effect without using
NestList, then why use it? And if it's not possible to get the effect without
using NestList, then how would you ever find out that you can use NestList?

It just seems like the actual programming experience would be a lot more about
hunting-and-pecking through documentation and google searches, rather than
reasoning your way to solution.

I'd love to hear from someone skilled with Wolfram to offer some clarity. Are
you just basically thumbing through the documentation all the time to find
that one perfect function call?

~~~
programnature
You are the target audience of this book. The Wolfram Language has gotten
huge, making it harder for beginners to find their way around the basics via
reference docs.

That said, WL is in fact based on pretty simple primitives. If you are
familiar with functional programming, things like NestList are familiar and
you'll start learning by finding the analogues of your favorite FP constructs.

WL also has a lot of functions that you might call "optional": those that
could be restated as simple combinations of other functions. The rationale
there is pretty simple: if there is a well-defined, commonly used chunk of
computation, it should be given a name (and design). The fact that a fair
amount of work is put into both the name and design of these things gives a
higher sense of coherence (and predictability) than you might expect from
their number.

------
murbard2
"The Wolfram Language has about 5000 built-in functions. All have names in
which each word starts with a capital letter."

Amazingly, I get the impression they say it as a point of pride, rather than
as an embarrassing design decision. Don't get me wrong, having a rich, large
standard library is great, but unless you use some kind of hierarchical
namespaces, it's impossible to deal with.

~~~
brandonbloom
You speak a language with three to six thousand "common" words and over one
million total words. English speakers utilize no such hierarchical naming
scheme, only practice.

Typical Mathematica notebooks (I won't say "programs") use a fair bit less
than one hundred of the common functions, plus a handful of domain-specific
functions from that the greater set of five thousand. At any time, you can
enter ?FunctionName to access documentation far more comprehensive than most
languages offer.

The UpperCaseForStandardLibrary and lowerCaseForYourCode convention actually
makes a lot of sense in the context of Mathematica's primary use cases vs
traditional software engineering projects. When I'm just trying to solve some
problem in Mathematica, using my computer as a bicycle for the mind, I
appreciate not having to putz with import statements or avoid name collisions.

~~~
murbard2
There's no reason to expect a language like English which evolved organically
to have an elegant or efficient design.

~~~
baddox
And thus, given the large amount of effective communication that occurs in
English, there's no reason to expect that there is much of a downside in
eschewing this particular language design approach that you propose.

~~~
bachmeier
> given the large amount of effective communication that occurs in English

I wasn't aware that anyone thought English was an effective language for
communicating. It's horrible. Even native English speakers have problems
communicating all the time.

~~~
PeCaN
Actually, English has a fairly high entropy/syllables ratio. [1][2]

Also, given that we can infer with decent accuracy what people are saying even
in noisy environments, I would say it's really not too bad.

You could do better of course, but in a good spoke natural language you want a
good mix of density (for efficient communication) and redundancy (for dealing
with environmental variables). Optimizing for one comes at the expense of the
other, and English strikes a pretty decent balance. [citation needed]

1: Pellegrino, et al. University of Lyons, 2011 <[http://www.ddl.ish-
lyon.cnrs.fr/fulltext/pellegrino/Pellegri...](http://www.ddl.ish-
lyon.cnrs.fr/fulltext/pellegrino/Pellegrino_to%20appear_Language.pdf>) 2:
There has been some valid criticism made to [1].

~~~
SilasX
The issue is _understanding_ , not _transcribing_.

The existence of politicians (and politics) proves that it's easy to
misunderstand.

------
cbd1984
So the only implementations are Wolfram and the cloud Wolfram, both of which
can go away or change incompatibly at any point, making my software worthless.
I fail to see why I should invest a lot of time in this, given that I'd then
have the Sword of Damocles hanging over my head in that fashion.

------
yogeshgirdhar
As a computer science PhD, I love Mathematica, the language. Also I love the
documentation it has, which makes hacking on it so much fun. However, I hate
its notebook interface, which I think is extremely buggy and sluggish. That
was the main reason I stopped using it a two years ago.

~~~
bloaf
I actually think it is a fantastic idea, but a mediocre implementation.

------
chinhodado
I don't really understand the point of the Wolfram Language. It feels like a
language with a humongous standard library that tries to cover everything?

~~~
rtkwe
It's purpose is much the same as Maple or R a language designed with
mathematics and stats. The standard library is just one of the peculiarities
of how Wolfram decided to design his language. Instead of using libraries like
R it's all built in. Beneath that it's not so terribly odd being a mostly
functional language with a standard library that covers maths requirements.

~~~
brandonbloom
Beyond surface similarities like an emphasis on data transformation over side-
effects, I wouldn't call Mathematica a functional language. It's much better
classified as a term rewriting language:
[https://en.wikipedia.org/wiki/Category:Term-
rewriting_progra...](https://en.wikipedia.org/wiki/Category:Term-
rewriting_programming_languages)

~~~
4ad
Then I guess you don't call Lisp a functional language either?

~~~
brandonbloom
Lisp's evaluation strategy is based on a very particular category of rewriting
systems known as the lambda calculus. Mathematica's is an abstract rewrite
system with a broad set of evaluation strategies that are not easily
represented in the lambda calculus (without first implementing a term
rewriting system).

------
oskarth
I've only used it briefly, but _Mathematica: A Problem-Centered Approach_
([http://www.amazon.com/gp/product/1849962502](http://www.amazon.com/gp/product/1849962502))
is a pretty good resource for learning practical (mathematical) Mathematica
programming.

------
1024core
Can you use this language outside of Mathematica, or without purchasing
Mathematica?

~~~
sundarurfriend
The FAQ[1] has this:

> How is it licensed?

> The Wolfram Language has multiple licensing models depending on usage
> scenario. It is available free for certain casual use in the cloud, in CDF
> Player, and on systems such as Raspberry Pi. It is available through site
> licenses at educational institutions. It is also available in a variety of
> subscription and paid-up product offerings. The Wolfram Language is also
> licensed for OEM use, embedded in hardware or software systems.

The "casual use in the cloud" may be referring to this:
[http://programming.wolframcloud.com/](http://programming.wolframcloud.com/)
(requires sign-up, haven't tried it), and I'm not sure how you'd go about
grabbing the Raspberry Pi edition.

[1]
[http://www.wolfram.com/language/faq/](http://www.wolfram.com/language/faq/)

~~~
GFK_of_xmaspast
> I'm not sure how you'd go about grabbing the Raspberry Pi edition.

Easiest way is probably to start by buying a RPi.

