Re: showing your work in mathematica
- To: mathgroup at smc.vnet.net
- Subject: [mg69858] Re: showing your work in mathematica
- From: dimmechan at yahoo.com
- Date: Tue, 26 Sep 2006 00:59:13 -0400 (EDT)
- References: <ef8262$bt9$1@smc.vnet.net>
Hi David. Following the solution you gave to Guillermo Sanchez (http://groups.google.gr/group/comp.soft-sys.math.mathematica/browse_thread/thread/ 95091e339ee5ab30/2d17c6a8eb01882e?hl=el#2d17c6a8eb01882e) 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 funFunc[lst] {{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 steps? As you said Trace and its variations with options is proper for debbuging. E.g. Trace[funFunc[lst]] 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 way. Anyway, I guess something like the following is not very welcome. funFunc[(dat_)?ListQ] := (Print[StringJoin["the starting list:\n", ToString[dat]]]; Print[StringJoin["sorting of list:\n", ToString[Sort[dat]]]]; Print[StringJoin["splitting of list on equality in the first element:\n", 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] & ]) funFunc[lst] 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: {{a1,b1},{a2,b2+b3+b4},{a3,b5+b6}} 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?) Regards Dimitris