Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

I have been using Mathematica for about a year. It is the first language that I have attempted to learn. I'm still very much a newbie, but there are moments I feel more like I am waving than drowning. As with many addictions, at first it left an unpleasant taste, but with time, using Mathematica started to open up new possibilities and I have come to depend on it.

It has occurred to me that there are a number of reasons why at some stage I would like to learn some new language(s) to complement Mathematica and further nurture my 'coding brain'. At times I feel slightly handicapped by not really understanding the capabilities/pitfalls of, for example, Do loops and other constructs that seem common in many languages. Indeed, it would be nice to be able to understand/relate to programmers that don't use Mathematica.

Although a lot of coding paradigms can be used in Mathematica, I feel it would be instructive to spend some time learning strictly procedural, object-oriented, etc. programming styles in the context of another language. Which other programming languages should a Mathematica-only user be interested in, so as to appreciate the underlying programming principles and constructs that one takes for granted with Mathematica?

Alternatively, I understand that there are a number of languages that can be implemented or interfaced from within Mathematica. Would it be a worthwhile trying to learn other languages/coding styles without leaving the notebook environment?

share|improve this question
This doesn't appear to be a question about Mathematica as such. Moreover, I would say that in the plethora of languages that exist, it depends what you might want to do with them as to which ones you should chose. You could learn about Do loops in a few minutes in a way which is independent of any particular language (or indeed within Mathematica) but the desired implementation should perhaps drive the tool of choice. Right now there are many nice languages being taught online, see for example codecademy.com, which might be an easy way to start. –  Jonathan Shock May 23 '13 at 2:34
As quite a few gentlemen here I am in favor of keeping the question - it is rather an interesting topic. –  Vitaliy Kaurov May 23 '13 at 3:34
FWIW, I vote against closing. –  J. M. May 23 '13 at 11:33
I don't want to close this question, but I would ask that people posting answers address some key issues that would be relevant to Mathematica users: (1) integration with Mathematica; (2) learning curve for someone already familiar with Mathematica and functional programming; (3) value-add of things Mathematica doesn't do so well (o/o, performance etc). –  Verbeia May 23 '13 at 13:33
@Jens I think this question is of that rare type which,while having no definite answer, may allow us as a community to reflect our place in the programming universe. So, just for this alone, I would make an exception and keep it. Perhaps, this is a close proxy to another question which many of us might be interested in: how does one productively expand his/her programming skills to be able to do more powerful things, having Mathematica as a base, and so that this base background is used most effectively. This is pretty much what was asked. –  Leonid Shifrin May 23 '13 at 15:23
show 11 more comments

We're looking for long answers that provide some explanation and context. Don't just give a one-line answer; explain why your answer is right, ideally with citations. Answers that don't include explanations may be removed.

8 Answers

A personal view

I was in a similar situation about 5 years ago. By then, I knew Mathematica well, but not much of anything else (I had some prior experience with a number of languages, but that was from the academic, rather than programming, perspective).

What I did was to learn C. I did it because I knew that otherwise I would be forever scared of low-level stuff, memory management, etc. High-level languages bring you the comfort of not worrying about low-level issues, but it is still good to be exposed to them at some point. I still think this was a right move, and would recommend learning C.

Where to go from there is a personal thing, and depends greatly on the goals you set for yourself. If you have some free time and want to learn some all-around useful and practical language, I'd then go with Python. If you have even more free time, some Lisp flavor would be great to learn.

But if you want to learn something really practical where lots of active development is going on right now, learn Javascript. It is rapidly becoming the assembly language for the web, in the sense that more and more languages compile to it as a target. It is a cool language in its own right, with all its short-comings, and it is very widely used. Some people consider it a Lisp wrapped in C-like syntax. I can certainly say from my own experience with it that it doesn't get in the way and is very fluid and nice to work with. The good thing about Javascript is that you can use it for OO and for functional programming, and there are currently tons of projects people are actively working on in Javascript, so you can pick some projects and learn from them.

How to leverage Mathematica

Those considerations I gave above are rather general, and more based on my personal story and views. But there are objective matters here too, many of which were mentioned in other answers. I would still add here my version of these arguments.

There are two "dimensions" where your Mathematica background may help (or, in any case, will be important). First is to become a better programmer through learning and using powerful abstractions, ways of thinking and programming techniques. For this direction, it is crucial to critically assess what Mathematica is and is not, its strong and weak points (as a language), good habits and bad habits we acquire as a result of doing some serious development in it.

Becoming a better programmer

I will mention a few strong and weak points, and the languages which would either share a strong point or help with the weak one

Strong points include

  • Ultra-rapid development due to multi-paradigm dynamic language. The characteristic features are

    • Bottom-up development,
    • Using immutable code and functional programming

    Pretty much any language of the Lisp family will share and reinforce this point, although perhaps Mathematica is unmatched here, both due to an extremely high-level nature and to its notebook interface. Python and Javascript will too, but they don't have such an explicit support for functional programming. Still, it is the dynamic and untyped nature of the language which is most important here, not the FP support per se

  • Many ways to solve a given problem. This is a mixed blessing, but for a beginner it is probably better than the opposite, since it encourages experimentation. I've heard that Perl and Ruby share this property. It seems important that the language is untyped and dynamic to have this.

  • Elegant and terse code for many problems - I've heard that some Lisp dialects (such as Arc) also have this feature. However, the Mathematica's type of terseness seems more of the type that strongly-typed languages like SML/OCaml/F# provide, or APL-family languages if you are interested in array-based operations.

  • Powerful meta-programming facilities. This comes from the facts that Mathematica is symbolic language, and also is homoiconic and supports the code-is-data paradigm. All or mmost Lisp-family languages also have this feature. At the same time, meta-programming in Mathematica is hindered by the lack of its widespread use in the community, and the lack of certain metaprogramming libraries which would make many things easier. But I think this is a temporary difficulty.

Some of the weaker points

  • Evaluation process too general and too complex for many programming problems. In other words, many or most programming problems don't require full symbolic power of the evaluator,but because you can't "switch off" part of it, there is a large mental overhead involved. In other words, Mathematica quite strongly violates principle of minimal surprise. I consider this as one of the weakest sides of Mathematica as a programming language, while at the same time one of the strongest sides of it as a general symbolic system. Almost any functional (or OO) language you pick will not have this problem to such extent.

  • Incomplete support of functional programming. What I mean here is that recursion and linked lists are not supported as a main idiomatic tool for thinking about programming. You can still use it, but not as natural as in other FP languages. Besides, lexical scoping is an emulation, which reflects itself both in "leaks" and the user's ability to break it, and in e.g. closure creation being essentially a hack using leaked Module variables, rather than officially-supported feature. In this respect, many well-known FP languages (e.g. both Lisp and ML families) will have a more clean foundation for learning more FP techniques.

  • No systematic tools to scale for larger code bases. Strongly -typed langauges (Java, Scala, F#, ML/OCaml, Haskell) use type system for that, Lisp-family languages use macros and closures, and OO-supporting languages (Python, Java) also use object-orientation (inheritance, polymorphism). Mathematica sort of is able to do all of this, but none is simple and automatic enough to be used as a tool. So, while scaling to larger code bases is certainly possible in Mathematica, it requires experience and discipline. In any case, I would certainly use other language to learn how this is done.

  • Performance and the lack of native general efficient data structures. This is a problem typical for scripting languages with two or more performance scales. To a lesser extent, it is present also in MATLAB and R, while most other languages (Python, Scala, Lisp-family languages, ML-family languages, Haskell, Javascript, C of course, etc) don't have this issue much or at all. I would certainly pick some of general-purpose languages to learn the algorithms/ data structures, although Mathematica can be extremely useful for this as well, when writing prototypes (it just has often unacceptable time constants for many simple implementations). I think that data structures are best learned either with C or some strongly-typed language (ML/Ocaml/F#, Haskell).

Getting practical

In this respect, one very sensible suggestion was given by Vitaliy: try to use languages which have already (or potentially) a good linking with Mathematica. This will help a lot because you can use Mathematica as an incredible high-level testing and development environment for those languages.

All languages running on the JVM (Scala, Clojure, Groovy, Jython, JRuby) should have a very good potential for intergation with Mathematica, due to the existence of JLink (of course, Java is too). Of course, C is also well-integrated with Mathematica via MathLink and LibraryLink, and .Net - based languages too, via .Net/Link.

By learning these languages in integration with Mathematica, you will not just learn the languages, but add some more parts to the "technology stack" which you might be able to use in the future to create more complex systems, for which Mathematica alone (as well as other languages alone) may not be as good as their combination. While currently such integration tools are not yet well-developed, I feel Mathematica has a huge potential as a system integrator and a medium which may serve as a catalyst in rapidly producing hybrid systems written in a number of different languages. Traditionally, the majority of efforts for such systems seem to have been spent on overcoming cross-language barriers, but if all these languages are interconnected through Mathematica as a central "hub", this problem may be seriosly alleviated.

So, from this perspective, you may pick the language which is best - integrated with Mathematica - as Vitaliy already suggested.

share|improve this answer
I'd suggest C too, for the reasons you've stated. If nothing else, it will give geordie a new appreciation of Mathematica :-) –  Simon Woods May 23 '13 at 14:32
@SimonWoods Well, I really love C. It just so happens that recently I did not have many chances to work with it, but I hope that will change. Actually, I empirically found that I tend to really like languages which are good to both write code in them and to generate code automatically. From the langauges I know reasonably well and like a lot, all - Mathematica, C and Javascript - do have this property. –  Leonid Shifrin May 23 '13 at 14:52
Oh I didn't mean to imply that geordie would dislike C, just that having Mathematica as a first language it must be easy to underestimate just how much low-level stuff is going on behind the scenes. It sounds like I should be adding Javascript to the list of "languages I would like to learn but never seem to find the time to do so". (Currently Python is at the top of that list, Julia looks interesting too) –  Simon Woods May 23 '13 at 16:05
@SimonWoods Re:C - Sure, I totally agree. Re: Javascript - I think that given the current realities and the amount of good code / literature that became available for Javascript in the recent few years, it may serve the same purpose as Python served before (in purely pedagogical aspect) - a language high-level enough to not worry about some low-level stuff, and powerful enough to be able to do cool things quickly, and also supporting the majority of popular paradigms (object orientation is perhaps still best learned via Python, since JS's version of OO is rather peculiar). –  Leonid Shifrin May 23 '13 at 16:23
@Stefan Re:"hub" - yes, it won't be very easy, but I think it's quite possible, and also I feel that this direction has a huge potential. Generally, we seem to live in a very eclectic time, where a lot of knowledge and resources have been accumulated in narrow (sub)fields, while few attempts were made so far towards the synthesis. Since the common denominator must be a broad and permissive medium, imposing least possible contraints (in the first place, on thinking / expressing ourselves), I view Mathematica as a viable candidate for such an integration medium for programming. –  Leonid Shifrin May 23 '13 at 18:20
show 6 more comments

Leonid has presented a nice overview of some of MMA's weak and strong points. I'll present here some arguments why Python might be worth learning next. These are based on personal experience, since I did not get the point of Python until after I attended this workshop.

Go with Python. You'll be surprised how quickly it's possible to get some things done.

Python is a well designed object oriented general purpose dynamically typed scripting language. Apart from OOP it supports procedural and limited functional programing, so you will be able to practice a different way of writing programs.

Python currently has a lot of momentum in the scientific computing community: (see here, here, here for some nice introductions). The core scientific packages are Numpy, Scipy and matplotlib. But then there is a rich ecosystem for almost anything from HDF5 to antigravity:)

For most stuff Python is fast enough. Critical parts can easily be written in Cython or by hand in any low level language capable of producing a DLL.

Python is open-source and cross-platform.

Regarding MMA integration: I have no personal experience here. But there are options.

I'll sign off with a classic xkcd:

xkcd comic

On dark nights I sometimes regret my thesis code is not written in Python (but in MMA). Why you might ask? I'm doing some numerical stuff on large vector arrays ($10^7$ of 2D points). Somewhere deep down MMA unpacks them for a short while and eats GB of RAM. Loading a 40MB text file takes 3 GB. There is no real pass-by-reference (can be faked with HoldFirst), so I never know when copies are made. It's not open source or widely used, which makes the code I publish along with any article less attractive.

Don't get me wrong, MMA is the second language I learned (with Object Pascal being first) and I like it. But if I knew then what I know now, I'd go with Python.

share|improve this answer
I completely agree with Ajasja. Python is go-go for the newcomer. 1. written code easily understandable. 2. Gradual learning curve: Whatever you want to do with python, you can do it. You can easily start with scripts, then use list and databases, handle processes, threads, exceptions, object orientation. You don't have to be afraid that there is something that can't be done in python, but it's very easy to start using it. –  Kobor42 May 23 '13 at 12:41
I upvoted this because one has to know at least one non-commercial language in order to be more independent and mobile. –  Jens May 23 '13 at 18:10
@MarkMcClure Does the edit help? –  Ajasja May 24 '13 at 17:39
It's definitely better (+1). I cannot stress how much I hate hidden unpacking of lists, and very recently, I was in the same situation as you — I had a 3GB matrix, which after a series of transformations, was reduced to a 1GB matrix. Somewhere along the line, some hidden unpacking occurred which blew up memory usage to 11GB and was extremely hard to debug. It's under control now (and the changes made were minimal and subtle), but it was very frustrating to see otherwise "clean" code result in terrible performance (2-3 hrs vs. 60s after fixing) –  rm -rf May 24 '13 at 21:57
I agree about the copying issues, but I think Python suffers from this almost as badly at times. For example, "advanced indexing" into an ndarray produces a copy of the data, and this operation seems to be used quite often internally (which is not surprising given how powerful it is). I've written programs using advanced indexing that run quickly with datasets below some threshold size but slow down dramatically when this threshold is exceeded, apparently due to some sort of memory management issue related to this copying. –  Oleksandr R. May 26 '13 at 2:07
show 2 more comments

I think similarities and differences between the languages is very interesting discussion and I am glad Andrew replied something in that manner - I hope comments/answers will follow ;) On the other hand, concentrating rather on practical matters - a natural choice seems the languages that Mathematica is linked with. While you keep sharpening your Mathematica skill, you might find beneficial to learn, for example, C and then compile to it for speed in some cases. Wouldn't be also wonderful to master CUDA and OpenCL to employ GPUs in your Mathematica programming? R, Java, .NET - may come handy in some specific cases - so I'd recommend to read through:

Systems Interfaces & Deployment

Also related recent interesting discussion here - what a fun read:

Why Would a Mathematica User Care about R?

share|improve this answer
+1 This is really the kind of information I'm looking for. –  geordie May 23 '13 at 3:34
The note on 'linking to R' is really interesting. –  geordie May 23 '13 at 11:40
add comment

I knew more than a dozen languages before I learn Mathematica, so just like Andrew, I am also surprised that Mathematica is your first language. But, you are such a lucky person that your supervisor uses Mathematica, not Matlab (I am not joking)! There is not a simple answer to your question, because if there was a single best programming language, we shouldn't have had so many of them out there.

First of all, I should mention that, in theory, almost all of the programming languages have the same power. They are all Turing Complete. So, essentially, every program which is written in Mathematica, could also be written in C, C++, Python, Prolog, Haskell, or Java. However, in practice, each programming language is good in doing a few things and bad in other ones.

Second, why should someone learn a new language? (1) Sometimes, you want to do something that Mathematica is not the best language for. (2) Some of the techniques that can be helpful in your favourite language are much more obvious or focused-on in other languages. For instance, I learned about some of the list operations such as folding, mapping, zipping, and pattern matching when I was using Haskell, but I use them frequently in Ruby and Groovy. My skills in other programming languages are helpful while I am programming in Mathematica as well.

Third, what programming language should you learn next? Well, it really depends on your job and your interests. If you are always solving equations symbolically or plotting them, stick with Mathematica. Otherwise, my short answer is: Python or Ruby are good choices for the first step. Java and Scala are good for the next step.

A long answer follows. Mathematica is a functional (but not purely function), untyped, interpreted, symbolic language. The programming environment (notebooks) of Mathematica is also different from pretty much any other programming language I have ever seen (maybe Maple is the only similar one that I remember).

There are generally two types of programming languages: Declarative languages (in which the program specifies what is the desired output, but not how to compute it) and imperative languages (in which the program specifies how to compute it as well). Functional languages (such as Haskell and SML) and logic programming languages (such as Prolog) are in the first category while most of the well-known programming languages such as C/C++, Python, Java, Fortran, Pascal, Basic, ... fall into the second category. Mathematica falls somewhere in between because it has some of the features of the functional languages (such as pattern matching and higher order functions) and some of the features of procedural languages (Do/While loops and mutable variables, for instance). So, learning a purely functional or purely imperative language would be instructive.

Another important aspect of any programming language is its typing system. There are two different semantics that are being confused very often: (1) Static typing (i.e., type of a variable cannot change) vs. dynamic typing. (2) loosely typed (i.e., any operator/function can be applied to any any variable/constant) vs. strongly typed. As an example, Java is a statically and strongly typed language. If you define the type of a variable as integer, you cannot (1) change the type, (2) assign a float value to it, and (3) pass it to a function that accepts strings. Mathematica typing system is totally unclear for me. I can say it's an untyped language since everything is considered as an expression in Mathematica. You can apply any (defined or undefined) function on any expression. But, in some other sense, you can use patterns to specify the types or heads (e.g., Integer, Real, List, ...). So, I would say it's something in between loosely/dynamically typed and untyped. So, learning a statically and strongly typed language, such as Java or Scala, would be helpful too.

share|improve this answer
Do you have any view on Scala vs. F#? I have heard good things about both of these but have no practical experience of either. –  Oleksandr R. May 23 '13 at 13:26
I love Scala but I don't know really so much about F#. Scala is quite a complicated language since it combines OO with functional programming (and it does it in a good manner). Learning Scala is not as easy as learning Python for sure. About F#, you should ask yourself if you want to put your eggs in Microsoft's basket or not. –  Mohsen May 23 '13 at 13:34
@Mohsen from my opinion this is the best answer so far although it DOES NOT MENTION PERL AT ALL!! :) Where I can't agree with is that the experience in other programming languages helped with Mathematica. Very often I am struggling because I expected a totally different behavior and had to force myself towards Mma idiosyncrasies... Said that...Kudos! Very good answer indeed...upvote! –  Stefan May 23 '13 at 15:02
@Stefan: Thanks Stefan. About Perl: I was surprised by Perl for a while before I knew Ruby. Believe it or not, Perl's programs are messy and unreadable. Yes, you can convert a 50 lines C++ program to a 5 line Perl code. But, if you want to write large scale maintainable programs with perl, you would be disappointed. Go read about Ruby ruby-lang.org/en/about. Larry Wall (the creator of Perl) is the hero of Yukihiro “Matz” Matsumoto (the creator of Ruby). –  Mohsen May 23 '13 at 19:21
About the second part: I agree with you to some extent about the Mathematica's different behaviour. But just assume you already know about the fast-fail nature of some languages like Java, C++, C#, or Pascal and then you move to Mma. You might be able to change the Mma's slow-fail behaviour by a bit of extra effort. –  Mohsen May 23 '13 at 19:27
add comment


The simplistic answer is install a copy of Dartmouth Basic or Pascal and fiddle with it. Not necessarily a bad starting point, but I think a far more detailed subjective approach will offer longer term benefits. For what its worth I'll share my experience.

So, for me, learning other languages was and is a practical matter dependent on motivation and purpose, what you're doing, who you're doing it for and most important longer term considerations of what you want to do - the proverbial 'where do you want to be in five years'. Operating at the quanta level in some cosmic enterprise may immediately require various language skills, specifically mandated or clear in context. And, of course, in that situation it's important to be able to hold your own professionally. In contrast, in a start-up, Mathematica is very helpful stand-alone or to model and prototype for other coding. In this case, other code depends on your circumstances and is less motivated by competitive professional pressures.

My second act was a start-up, and like you, Mathematica was my first programming language. I had started a boutique fixed income money management firm in 1989. Having come from twenty years at a large Wall Street firm with state of the art analytics, I understood that the ability to 'roll my own' analytics translated to a totally customized solution for clients.

As I researched languages and software, I tripped over an odd duo in a Palo Alto Mac software store, Wolfram and Theo Gray peddling Mathematica (literally, shirts sleeves rolled up talking to anyone who would listen, including me.) Wolfram made noises about aspects of his product that made little sense at the time, but when he talked about efficient programming relative to other languages, I plunked down my $299 and took home the package, v1 Mathematica. I suffered an horrendous learning curve ... many years.

But, by the mid-nineties I was writing Mathematica packages which included the usual suspects of fixed income packages, options and futures packages capable of moderate data access, a GARCH package (with significant help from Wolfram) and some customized indexes. I was quite pleased with myself, competitive, the clients were happy, billings were paid promptly.

By that time it was apparent that APIs might have value. So I fiddled with MathLink and learned some C. The number of pertinent vertical apps was too small to justify the time commitment. My needs were satisfied almost entirely with Mathematica.

From day one I had a significant problem with Mathematica and large data sets. ReadList and later Import were slow and painful, streaming was better but not much and the SQL interface introduced in v4 was better but marginally so. The problem persisted through 2005. In the last eight odd years, with the advent of Big Data solutions in the guise of Hadoop and its offspring and platforms like Talend Open Studio, my demands on Mathematica have morphed.

Being able to write Mathematica command line scripts has become invaluable. For example, Talend is a marvelous platform, but some of its GUI components lack granularity. In those cases it is far more efficient to write on the fly scripts than create new Java components for things like date and string parsing, analytics and file handling. You need some Unix which if you don't have you should acquire. (see Powers, Peek, O'Reilly, Loukides "Unix Power Tools")

Over the last eighteen-months I have happily learned enough Java and J/Link to effectively interact with in-memory database solutions, in this case VOLTDB, which is written in Java. This solution is exponentially faster than disk based solutions with an enormous benefit. I can write my own GUI in Mathematica.

Over the years, I've looked at a variety of languages and longed to learn them all. My work with Mathematica has allowed me to conceptualize various programming paradigms and techniques. And I've received invaluable help from folks like Roman Maeder, Stan Wagon, David Wagner, Patrick Tam, Tom Wickham-Jones, Hal Varion, Heikki Ruskeepaa, and Sal Mangano.They all make it look oohh so easy. If you can get their books you should. I have always found the cookbook formats to be useful. If it's practical for you, you might take a look at O'Reilly's SafariBooksOnline.com .

I would find an efficient way to grasp the basics (the Basic Pascal approach) but then be driven by circumstances and desire. And read read read.

Best of luck.

share|improve this answer
Welcome to the site! What a great story--it sounds like Mathematica appeared in just the right place at the right time for you. In the interests of making the advice more appropriate to the modern day than the 1970s, though, I wonder if Pascal and Dartmouth BASIC ought to be replaced by Ruby and Python? Even though my first language was BASIC, I wouldn't recommend it to anyone today. Fortunately, there are far better languages available now, from which one can learn much more, far more quickly, and without developing bad habits. –  Oleksandr R. May 30 '13 at 22:33
Tks, I was fortunate. Mathematica was very helpful in so many ways. I dare say at the very least Python with which I've spent some time would be better. I don't know much about Ruby. –  user7708 May 31 '13 at 0:57
add comment

It's really fascinating to hear from someone who learned Mathematica first! This is something I have thought about a bit. Among mainstream languages, I think the overall closest in spirit is Scala.

Selected similarities:

  • Case classes for rudimentary pattern matching
  • A strong emphasis on functional programming

Selected differences:

  • Strong type checking
  • Scala, like most non-Lispy languages, is not homoiconic
share|improve this answer
+1 I think this could be an interesting discussion ;) –  Vitaliy Kaurov May 23 '13 at 3:19
Considering how blurry the boundary between code and data is in mma, I take it then that mma is homoiconic? –  rcollyer May 23 '13 at 3:20
It was mentioned in the question that it would be good to explore procedural languages. My knowledge of these things is limited, but it seems that Scala is very much functional. This doesn't mean that this isn't a good answer, but I think it's important to note how this does and does not satisfy the question. –  Jonathan Shock May 23 '13 at 3:42
add comment

There are so many interesting programming languages to choose from that you would need more than one lifetime to explore them all. Going back to the Lisp origins of Mathematica would lead you to Clojure, to Scheme and the practical Racket, and then to Common Lisp, which is a behemoth a bit like Mathematica, and there's plenty there to stretch your brain cells (and to remind of you of the early days of learning Mathematica).

The book by David Wagner, "Power Programming with Mathematica", now freely-available thanks to the efforts of Todd et al. shows this table of correspondences between Lisp and Mathematica:

enter image description here

so your experience with functional programming will give you a head start.

Haskell is interesting. Here's the basic Fibonacci:

fib :: Integer -> Integer
fib 0 = 1
fib 1 = 1
fib n = fib (n-1) + fib (n-2)

compared with a Mathematica definition:

fib[0] = 1;
fib[1] = 1; 
fib[n_] := fib[n - 1] + fib[n -2]; 

and mapping a Haskell 'pure function' over a list:

 map (\x -> x + 1) [1,2,3,4]

Or you could do something unpredictable and look at Factor, which will get you thinking backwards.

share|improve this answer
If we're talking about research languages, maybe it's also worth mentioning Maude, a term rewriting system, and Clean, a dynamically typed pure functional language. –  Oleksandr R. May 23 '13 at 13:05
And Julia as well... –  Ajasja May 24 '13 at 17:47
@OleksandrR. I was looking at Pure the other day. As far as I know, it's the only general purpose language based on term rewriting that has a potential of being useful. (It's predecessor, Q, is not developed any more.) It even has some math libraries (a GSL interface). –  Szabolcs May 30 '13 at 20:17
add comment

I'm pretty sad that no one has mentioned Perl!

Since more than 20 years Perl is the fastest and most efficient dynamic language.

A lot of people say that Perl's syntax is unreadable or crude. I say it is readable indeed. Syntax follows semantics ;)

With Perl you're back in time where men were men and wrote their own primality test:

sub is_prime {
    my ($number) = @_;
        return (1 x $number) !~ m/\A (?: 1? | (11+?) (?> \1+ ) ) \Z/xms;


sub is_prime {
    print "Prime!\n" if (1 x shift) !~ /^1?$|^(11+?)\1+$/


Just to turn my post into a popular one (sigh), here is the Mma equivalent of the regex prime checker:

PrimeRxQ[number_Integer] := Not @



Just because some mentioned the capabilities of other languages to support imperative and functional style of coding, I'd like to show how easy it is to achieve something similar with Perl.

Functional programming


currying is a technique which transforms an ordinary function into a function factory for manufacturing more functions.

Perl's built in functions map / grep are higher-order functions because each of them takes an argument that is itself another function.

map { $_ * 2 } (1..5)           # returns 2, 4, 6, 8
    grep { $_ % 2 == 0 } (1..10)    # returns 2, 4, 6, 8

A curried function of these functions is often more convenient:

sub cmap(&) {
    my $f = shift;
        my $r = sub {
        my @result;
        for(@_) {
            push @result, $f->($_);
    return $r;

sub cgrep(&) {
    my $f = shift;
        my $r = sub {
        my @result;
        for(@_) {
            push @result, $_ if $f->($_);
        return $r;

these functions can be called like this:

$double = cmap { $_ * 2 };
$evens  = cgrep { $_ % 2 == 0 };

after which:

$double->(1..5) returns (2, 4, 6, 8, 10)


$evens->(1..10) returns (2, 4, 6, 8, 10)

If we talk about other functional idioms (memoizing, automatic currying, foldl, foldr etc.) these can be easily achieved as well with the

P athologically E clectic R ubbish L ister :)

And last but not least! Perl runs nearly everywhere. Perl is one of the most portable programming environments available today. (just to toot in another superlative horn)

Edit 2

As others did, I leave you as well with a classic picture from xkcd:


Edit end

share|improve this answer
I guess your primality tests are based on the regular expression primality checker. Cute, but I'm not sure that's the right code to convince the uninitiated that perl is readable. :) –  Mark McClure May 23 '13 at 12:06
well the second one is the regex-checker made famous by Abigail: perl -wle 'print "Prime" if (1 x shift) !~ /^1?$|^(11+?)\1+$/' [number] the first is based on the same principle, but cleaned and hardened up. –  Stefan May 23 '13 at 12:15
The numbers are self explanatory. 0 upvotes for perl, 6 upvotes for python. I am sure perl is efficient, but this post is about learning curves - and perl is one of the worst candidates here. –  Kobor42 May 23 '13 at 12:45
To be clear, I rather like Perl for some of the same reasons that I like Mathematica. Both of them allow you to specify a program using a very small number of symbols and figuring out the way to do so can be quite fun. I used Perl quite a bit back in the mid 90s when I was first trying to seriously interact with students over the web. At that time, CGI via Perl or C was the only way to go. However, both Perl and Mathematica are rather domain specific and I don't know that I'd use either too much outside of their originally intended domain. –  Mark McClure May 23 '13 at 13:58
Mathematica has tried to capture some Perl mojo with its regular expression capabilities, and one can write pretty opaque code in Mathematica too, so I can see the overlap... –  Jens May 23 '13 at 18:14
show 7 more comments

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.