When I use some function, at the end along with the output I get some NULLs returned that I don't understand the reason for and are highly undesirable. For example, when I define the reversal of list with

ReverseList[ele_List] := Module[{list = List[], i, k = 1},
  {For[i = Length[ele], i > 0, {i--, k++}, {AppendTo[list, ele[[i]]]}],

The output I get

 ReverseList[{1, 2, 3, 4, 5}]


{5,4,3,2,1} {Null, Null, Return[{5, 4, 3, 2, 1}]}

I am also have a problem with returning the list. Can someone please help me with these two problems?

  • 1
    $\begingroup$ I'm assuming this is just a toy example, but in case you weren't aware, there's also Reverse@list $\endgroup$ – rm -rf May 19 '13 at 19:47
  • $\begingroup$ I was just practicing with syntax to be familiar with.. $\endgroup$ – Rorschach May 19 '13 at 19:51
  • $\begingroup$ Only module parameters need to be presented as a list, not the body. $\endgroup$ – SEngstrom May 19 '13 at 21:01

I think I understand what your trouble might be. Here is your code:

enter image description here

a := b means that the left-hand-side is a pattern and the right-hand-side should be evaluated when that pattern is found (roughly speaking). The right-hand-side in this case is:

enter image description here

Your function in essence returns the entire module. You can see this by changing Module to something else, like PretendModule:

enter image description here

The point is that Mathematica is a language where everything is an expression and everything automatically returns something. In other words, you almost never need to use Return.

So your function automatically "returns" the entire Module. But in this case, Module itself does its own stuff, and returns its body after it has done its variable stuff:

enter image description here

As you can see, the body is a list. This Module will return that list because that's its body and that's Module's job. However, I see what you are trying to do here. You don't need to wrap things in lists because when you use ;, expressions are automatically combined. Run FullForm[Hold[Print[1]; Print[2]]] and you will get:

Hold[CompoundExpression[Print[1], Print[2]]]

So ; is syntactic sugar for CompoundExpression. That's why you don't need to wrap things in lists. The operator ; automatically combines separate expressions into single expressions. The compound expression itself will return the last expression's value when it is evaluated. So your code can be changed to:

ReverseList[ele_List] := Module[{list = List[], i, k = 1},
  For[i = Length[ele], i > 0, i--; k++, AppendTo[list, ele[[i]]]];
  • 3
    $\begingroup$ Good explanation! $\endgroup$ – cormullion May 19 '13 at 21:52
  • 1
    $\begingroup$ I agree. This is an excellent answer and worthy of reference. Please consider posting a generic form of this answer here. $\endgroup$ – Mr.Wizard May 21 '13 at 7:19
  • $\begingroup$ @Mr.Wizard might literally take me a couple weeks, but will do $\endgroup$ – amr May 21 '13 at 20:15

Print returns Null, and you're returning a list of values, some of which are also Null. So:

ReverseList[ele_List] := 
 Module[{list = List[], i, k = 1},
       i = Length[ele], i > 0, {i--, k++},
       {AppendTo[list, ele[[i]]]}];

would be slightly better, perhaps?

(Obviously you wouldn't really reverse a list like this, but I presume you know that...)

Slightly more idiomatic than For and more generally useful is Table:

ReverseList[l_List] := 
 Module[{list = l}, Table[Part[list, i], {i,  Length[list], 1, -1}]] 
  • $\begingroup$ ya m just trying to be familiar with syntax $\endgroup$ – Rorschach May 19 '13 at 19:48
  • $\begingroup$ shall I always put ; instead of , in functions ? $\endgroup$ – Rorschach May 19 '13 at 19:48
  • 2
    $\begingroup$ Essential reading! $\endgroup$ – cormullion May 19 '13 at 19:49

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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