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've recently started a course in which I am learning how to solve different problems using Mathematica. A large part of the course covers creating your own functions. The thing is I don't like how my code looks.

Take for example the following:

CreateTuples[list_, size_] := 
  If[list != {},
      CreateTuples[Take[list, size-Length[list]], size],
      Take[list, size]
    ], (* Prepend *)
  ] (* If *)

This example is still small and simple, but already has some weird constructions like the {} in the If.

Are there any general tips or documents that describe some good styling rules for notebook files?

Also, while my example uses a function, my remarks also go for large expressions of other kinds. I just want to keep everything readable for myself and others.

But it might just be the software developer in me that is not used to bracketing like this.

share|improve this question
As long as the pure code-formatting issue is concerned, have a look here. I hope that very soon the code formatter described there will be much more usable. –  Leonid Shifrin Dec 4 '12 at 18:55
Personally I see no issue with Mathematica code not looking like C or some other language. I rather enjoy that it doesn't necessarily look that way. –  Sascha Dec 4 '12 at 18:57
You could get rid of the first empty list by replacing the condition with Length@lst!=0 and using CreateTuples[list_List,size_]. But is this what the problem is? –  acl Dec 4 '12 at 19:01
One thing I would recommend as matter of good style is to rename your function createTuples. Global symbols starting with uppercase characters should be reserved to the Mathematica developers. –  m_goldberg Dec 5 '12 at 0:52
@Willem, a more Mathematica-esque style would be to remove the If statement completely and terminate the recursion by defining a special case CreateTuples[{},_]:={} –  Simon Woods Dec 5 '12 at 10:44
show 5 more comments

Your Answer


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

Browse other questions tagged or ask your own question.