Mathematica 9 is now available
Services & Resources / Wolfram Forums
MathGroup Archive
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2006

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: showing your work in mathematica

  • To: mathgroup at
  • Subject: [mg69858] Re: showing your work in mathematica
  • From: dimmechan at
  • Date: Tue, 26 Sep 2006 00:59:13 -0400 (EDT)
  • References: <ef8262$bt9$>

Hi David.

Following the solution you gave to Guillermo Sanchez


I defined the following one-liner

funFunc[(dat_)?ListQ] := ({#1[[1,1]], Last[Total[#1]]} & ) /@
 Split[Sort[dat], First[#1] === First[#2] & ]

Here is one test list

lst={{a1, b1},{a2,b2},{a2,b3},{a2,b4},{a3, b5}, {a3, b6}} ;

And here is the (desired) result

{{a1, b1}, {a2, b2 + b3 + b4}, {a3, b5 + b6}}

Is is possiple to insert these Print statements inside
the definition of the funFunc function so that everytime
we execute funFunc to see and (some of) the intermiadate

As you said Trace and its variations with options is proper for
debbuging. E.g.


Trace[funFunc[lst], Sort]

Trace[funFunc[lst], Split]

Trace[funFunc[lst], SameQ]

Trace[funFunc[lst], Total]

Of course someone will suggest TracePrint e.g.

TracePrint[funFunc[lst], SameQ]HoldForm[SameQ]

but I think it is better to define the function funFunc in a proper

Anyway, I guess something like the following is not very welcome.

funFunc[(dat_)?ListQ] := (Print[StringJoin["the starting list:\n",
Print[StringJoin["sorting of list:\n", ToString[Sort[dat]]]];
Print[StringJoin["splitting of list on equality in the first
ToString[Split[Sort[dat], First[#1] === First[#2] & ]]]];
Print["final reasult===>sum of the second elements in each group: "];
({#1[[1,1]], Last[Total[#1]]} & ) /@
Split[Sort[dat], First[#1] === First[#2] & ])

the starting list:
{{a1, b1}, {a2, b2}, {a2, b3}, {a2, b4}, {a3, b5}, {a3, b6}}
sorting of list:
{{a1, b1}, {a2, b2}, {a2, b3}, {a2, b4}, {a3, b5}, {a3, b6}}
splitting of list on equality in the first element:
{{{a1, b1}}, {{a2, b2}, {a2, b3}, {a2, b4}}, {{a3, b5}, {a3, b6}}}
final reasult===>sum of the second elements in each group:

By the way, everything here is in InputForm apart from
the last Output. When I convert it to InputForm it loses
its format so I left it in StandardForm. (Why?)

  • Prev by Date: Re: linear secod order homogeneous differential equation recursions
  • Next by Date: Re: why does not the Mathematica kernel seem to 'multi-task' between computations in different windows?
  • Previous by thread: Re: showing your work in mathematica
  • Next by thread: Sum elements in a list with conditions