views:

3544

answers:

12

As a mechanical engineering PhD student, I haven't had a great pedigree in programming as part of my “day job”. I started out in Matlab (having written some Hypercard and Applescript back in the day, and being introduced to Ada, of all things, in my 1st undergrad year), learned to program—if you can call it that—in (La)TeX; and finally discovered and fell for Mathematica.

Now I'm interested in learning a "real" programming language that I can enjoy in the same sort of style as Mathematica, which tries to stress functional programming since it seems to map more nicely to how certain kinds of mathematics can be written algorithmically.

So which functional language should I learn? I guess the obvious answer is “as many as possible”, but let's start out humble and give a single, well-considered option a good crack. I've heard good things about, say, Haskell and Scala, but I wonder if (given my non–computer science background) I'd be better off starting in more “grounded” territory and going with Ruby or Python (the latter having the big advantage of being used for Sage, which I'd also like to investigate…after my PhD).

Well, I guess this is pretty subjective, so perhaps I could rephrase: would it be better to start looking at Haskell (say) straight after an ad-hoc education to functional programming in Mathematica, or will I get more out of learning Python (say) first?


In reference to the question "what do I want to do with it?", I guess my answer is "fun, and learning more". I've got this list of languages that I'd like to look at, and I don't know how to trim them down. And I'd rather start with something a little higher-level than C simply so that I can be somewhat productive without having to re-invent many wheels for any code I'd like to write.

+10  A: 

I would recommend F# (see Wikipedia), as it is a full fledged .net/CLI language and therefore uses one of the state of the art programming libraries and can be combined with C# if necessary.

Ralph Rickenbach
A: 

Well I'd suggest starting with reading The C Programming Language by K&R and understand pointers. The programming techniques and styles shown in K&R are great to carry over to other languages. After that I'd go with python as it is often used in the plugins I have seen for math and graphics programs.

Tanj
Haha, nothing's more functional than bit twiddling, right? (function > form that is)
guns
In a python "fanboyant" world it's an act of courage to recommend C :P.
Andrei Ciobanu
+6  A: 

I rely on Python for my physics research needs and it works very well. Native complex number types is a great boon.

As well as SAGE which you mentioned, there is also Python XY which is a framework of packages useful for engineers/scientists, built around Eclipse using Python as its language.

From the website:

With Python(x,y), one can do [...]:
  • interactive calculations including for example 2D and 3D plotting or symbolic maths,
  • simple functionnal (sic.) programming (with MATLAB*-like syntax) as well as powerful object-oriented programming,
  • scientific projects development from the simplest script to the most sophisticated application thanks to Qt development framework and Eclipse platform,
  • parallel computing on multicore/processors computers or even clusters (with Parallel Python),
...and a lot more!

I haven't used it myself yet but I plan to when I begin my next project.

Brendan
+5  A: 

Hi Will

As you know I am a Mathematica programmer too but I dip my toes into all sorts of languages and environments thanks to my day job. If I were you I would definitely take the time to learn some Python. I did and have never looked back - my days using Perl are now just quickly fading memories.

When you write code in Mathematica you are spoiled since so much is done for you. You can take advantage of fancy plotting routines, complex numerical and symbolic algorithms, GUI components etc - the list just goes on and on. Moving to a lower level language can hurt when you are used to that level of integration.

It hurts a lot less when you try Python. You have access to fancy plotting routines (matplotlib), complex numerical and symbolic algorithms (SciPy, SymPy), GUI components (for example - easygui) etc

Ok so it's not functional (you can do FP I believe but I have never done so) but it IS fun and you can be productive with it in a short amount of time.

I guess it depends on what you want to achieve with your learning. If you are fascinated with the ideas of functional programming itself and don't really care about actually doing stuff then Python won't be for you. If you to do stuff quickly and actually enjoy the process as you go then Python is definitely for you.

Finally, since you are a Mechanical engineer, I'll mention that the 800 pound gorilla of finite element analysis packages - Abaqus - uses Python heavily. Oh..and since you have used MATLAB - a lot of MATLAB coders I know are starting to consider switching to Python.

MikeCroucher
+3  A: 

I would go for Erlang.

Originally developed by Ericson in the late '80's and early '90's, it is now opensourced. They do have a web-framework and a proven record in the telecom industry. Adapts wonderfully to Multi-processor environment because of it's light weight multi-threaded design and with all the multi-core pc's and servers around it could become one of the future languages to watch.

In the late 80's it was a language far ahead of its time.

Schalk Versteeg
+5  A: 

If you want to pick something rather close to Mathematica, I'd say Lisp/Scheme are really good, because the Head structure of Mathematica corresponds fairly closely to the lisp scheme; Plus[1,2] becomes (+ 1 2), so the head/arguments structure is really the same. Plus, you can learn about macros, which is like Mathematica's Hold functions on crack.

If you love the rule functionality of Mathematica, then an equational rewrite language such as Maude might be good to look at.

John the Statistician
A: 

A really good functional language to learn is Haskell. It's a very pure type safe functional language and is reasonable for representing mathematical constructs. Another one that is still under development is Fortress which is designed to look very similar to real mathematics.

Michael Barker
+3  A: 

It sounds like you'd learn more from Haskell and find Python more directly useful; either one's a good choice. Haskell offers a type-directed programming style using a fancy typesystem, a sort of development we'll see more of in newer advanced languages and reasoning systems like theorem provers. Others have already listed the advantages of Python (my main day-to-day programming language).

If you want to learn about CS and software development from some really great books, then my favorite language is the Lisp family: it offers books like PAIP, SICP, and EOPL. But there seems to be less going on in the Lisp world lately.

Darius Bacon
+2  A: 

You should consider Ruby too. Both Mathematica and Ruby are influenced by LISP. I have found that many of Mathematica functional programming features are available in ruby. For example

In[1]:= Select[Map[#^2 &, Range[10]], # > 50 &]
Out[1]= {64, 81, 100}

In Ruby it is

>> (1..10).collect{|x| x**2}.find_all{|x| x>50}
=> [64, 81, 100]
gdelfino
Well, for the sake of the example you can in fact write "map" and "select" in the Ruby code.
tokland
A: 

In addition to gdelfino answer above:

>>> filter(lambda _ : _ > 50, map(lambda _ : _**2, range(11)))
[64, 81, 100]

is the Python equivalent !

I just used _ instead of Mathematica's # to stand for anonymous argument. You can use filter( ) and map( ) to do most of FP things in Python.

bdsatish
or, for list comprehension fans: [x**2 for x in range(11) if x**2 > 50]
Mark
A: 

I would suggest such functional programming learning path (arranged from easiest to hardest):

newLISP -> Scheme -> Common Lisp

So in the end you will be guru of functional programming and as such you will be able to choose for yourself next node of your functional life (be it F#, OCaml, Haskell or anything else)

0x69
A: 

I use Mathematica, Fortran, C++, and C# on a regular basis. As far as getting the job done in the shortest amount of time, and the easiest way possible, Mathematica is the best hands down. I used to use MatLab but don't bother any more. It is a child compared to what Mathematica can do.

The only time I ever use anything else is when I need more speed or memory. If you need more speed, Mathematica can call directly to the CLR or Java, which is why learning C# or Java would be a good start. You can just write the one function in C# that is causing the bottleneck and call it. If you still need more speed C# and Java can both call Native and so Fortran is by far the fastest, or you can call some optimised library.

The only time when mathematica can't be used efficiently is when you are memory restricted...there is simply no way to efficiently control memory allocation with mathematica. In this case I use C++. OO is really a fantastic way to program and if you are looking for a real programming language then either C# or C++ should definitely be it. A well written piece of object oriented code is so simple, intuitive and easy to understand that you almost don't need any comments whatsoever. When your projects start getting really big is when it really shines: no matter how many lines of code you add the complexity of what you are doing increases very little.

Mcdf