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.

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]]]}],
   Print[list],
   Return[list]}
  ]

The output I get

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

is

{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?

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

2 Answers 2

up vote 4 down vote accepted

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]]]];
  list]
share|improve this answer
2  
Good explanation! –  cormullion May 19 '13 at 21:52
    
I agree. This is an excellent answer and worthy of reference. Please consider posting a generic form of this answer here. –  Mr.Wizard May 21 '13 at 7:19
    
@Mr.Wizard might literally take me a couple weeks, but will do –  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},
     For[
       i = Length[ele], i > 0, {i--, k++},
       {AppendTo[list, ele[[i]]]}];
     Print[list];
    list]

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}]] 
share|improve this answer
    
ya m just trying to be familiar with syntax –  Rorschach May 19 '13 at 19:48
    
shall I always put ; instead of , in functions ? –  Rorschach May 19 '13 at 19:48
2  
Essential reading! –  cormullion May 19 '13 at 19:49

Your Answer

 
discard

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.