2
$\begingroup$

I am currently studying the functional programming part of Mathematica. But I am very confused right now about what I have learned so far.

This because I saw an example in textbook using Do loop to swap list elements.

For example, original list :

lis = {{a, 1}, {b, 2}, {c, 3}}

Desired Result should be:

{{1, a}, {2, b}, {3, c}}

I try to write the program code as below, but no error, no result in the end.

 lis = {{a, 1}, {b, 2}, {c, 3}};

 Do[temp[i] = {lis[[i, 2]], lis[[i, 1]]}, {i, 1, Length[lis]}];

 temp

For fixing this issue, I tried to rewirte line two as:

 Do[temp[[i]] = {lis[[i, 2]], lis[[i, 1]]}, {i, 1, Length[lis]}];

But got errors as below which I also don't know why:

Set::noval: Symbol temp in part assignment does not have an immediate value.

General::stop: Further output of Set::write will be suppressed during this calculation.

So really appreciate anyone could clarify such problem. Thanks a ton!

$\endgroup$
4
  • 1
    $\begingroup$ Why not Map[Reverse, lis] ? $\endgroup$
    – Ray Shadow
    Commented Jun 27, 2017 at 18:49
  • 1
    $\begingroup$ because I follow the example from the textbook, so I got some further questions about this example. $\endgroup$
    – cj9435042
    Commented Jun 27, 2017 at 18:54
  • $\begingroup$ you would need to "initialize" temp as a list to use this approach, eg put temp = {0, 0, 0} or temp=ConstantArray[0,Length@lis] before the Do loop. $\endgroup$
    – george2079
    Commented Jun 27, 2017 at 18:57
  • 4
    $\begingroup$ I would not call this method 'functional'. See: Reverse element in nested list $\endgroup$
    – Kuba
    Commented Jun 27, 2017 at 19:00

4 Answers 4

3
$\begingroup$

Two small errors in your code...

 lis = {{a, 1}, {b, 2}, {c, 3}};

 Do[temp[i] = {lis[[i, 2]], lis[[i, 1]]}, {i, 1, Length[lis]}];

 temp

You need to initialize temp to the right size, and then it should be temp[[i]] not temp[i].

temp = lis = {{a, 1}, {b, 2}, {c, 3}};

Do[temp[[i]] = {lis[[i, 2]], lis[[i, 1]]}, {i, 1, Length[lis]}];

temp

(*    {{1, a}, {2, b}, {3, c}}    *)

I am guessing that this is the "before" example in your functional programming text?

$\endgroup$
3
  • $\begingroup$ Thanks for pointing out, I can understand the initialize process now. But why not temp[i] is still a confusion $\endgroup$
    – cj9435042
    Commented Jun 27, 2017 at 21:47
  • 1
    $\begingroup$ The only one which answers the question so far :) $\endgroup$
    – Kuba
    Commented Jun 28, 2017 at 6:19
  • 1
    $\begingroup$ cj, think of temp[i] as a call of the function temp[ ] with argument i, as opposed to temp[[i]] which referencing the ith element of a list temp. Your error that you didn't understand came about because temp needed to be initialized. @Kuba, sometimes folks are too clever by half. :) $\endgroup$
    – MikeY
    Commented Jun 28, 2017 at 13:23
1
$\begingroup$

Try

Table[{lis[[i, 2]], lis[[i, 1]]}, {i, 1, Length@lis}]

{{1, a}, {2, b}, {3, c}}

Or - a little bit more advanced

Map[Reverse, lis]

{{1, a}, {2, b}, {3, c}}

If you want to do this with Do:

rev = {};
Do[AppendTo[rev, {lis[[i, 2]], lis[[i, 1 ]]}], {i, 1, Length@lis}];

rev

{{1, a}, {2, b}, {3, c}}

The latter becomes rather slow with large lists.

$\endgroup$
1
  • 2
    $\begingroup$ Surely, if you were use Table, it would be Table[Reverse @ i, {i, lis}] and not the monster you show above. $\endgroup$
    – m_goldberg
    Commented Jun 27, 2017 at 22:15
1
$\begingroup$

Just some other ways:

{#2, #1} & @@@ lis

or

Transpose@*Reverse@*Transpose@lis
$\endgroup$
-1
$\begingroup$
Reverse[{{1, a}, {2, b}, {3, c}},2]

Try: Do[AppendTo[newlis, Reverse[lis[[i]]]], {i, 1, 3}]
 newlis starts as an empty list,newlis={}
$\endgroup$
1
  • $\begingroup$ Please, review your code, add any clarification notes, etc. $\endgroup$
    – Sektor
    Commented Jun 28, 2017 at 10:20

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

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