MathGroup Archive 2009

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

Search the Archive

Re: Conventional way of doing "struct"-like things?

  • To: mathgroup at
  • Subject: [mg104547] Re: [mg104490] Conventional way of doing "struct"-like things?
  • From: "David Park" <djmpark at>
  • Date: Tue, 3 Nov 2009 02:57:35 -0500 (EST)
  • References: <4047035.1257118894351.JavaMail.root@n11>


I would prefer your second method because it seems more natural and is just
a mnemonic shortcut for Part. Such simple definitions can be quite useful.
The method is also easily extended to more complex structures with sub-lists
or even conditional sub-lists.

As far as general Mathematica style is concerned you seem pretty savvy as it
is. You will probably get many suggestions so I'll throw in my two cents and
hope it doesn't too much conflict with the experts.

1) Prefer functional programming to procedural programming.

2) Localize variables in function definitions, With or Module statements.
Avoid setting Global values for simple symbols like x.

3) Keep things symbolic as long as possible.

4) You seem to come from a programming background. Just to nudge in a
different direction I would say don't think of Mathematica so much as a
programming language. Think of it as a piece of paper on which you are
writing and developing your ideas. Learn how to use sectional grouping and
text cells to provide discussion.

5) Build up generated knowledge. Write usage statements. Put developed
routines in a Routines section at the top of your notebook. Maybe you can
eventually transfer them from package purgatory to a real package.

David Park
djmpark at  

From: Erik Max Francis [mailto:max at] 

I find myself (as I'm sure everyone does) dealing with lists of 
structured data, e.g., a list of lists, each of which contains n 
elements with each entry representing a unique field.  What is the most 
conventional way to identify each of these elements when iterating over 
the whole list?  Obviously I can use [[...]]/Part, but I'm wondering if 
there's something considered more elegant.

Take a concrete example where I have a CSV containing ZIP code data, 
with seven fields:  the zip code, latitude, longitude, city, state, 
county, and class.  What's the usual way of addressing this?  I can 
think of two obvious ones; just define symbolic names for the indices:

data = Import[..., "CSV"];


ListPlot[{#[[LONGITUDE]], #[[LATITUDE]]} & /@ data,
  PlotStyle -> PointSize[0.001]]

or define functions (with or without the symbolic index names):

latitude[x_] := x[[2]];
longitude[x_] := x[[3]];

ListPlot[{longitude[#], latitude[#]} & /@ data,
  PlotStyle -> PointSize[0.001]]

What's consider more in Mathematica's style, or is there something else 
more commonly used I'm not thinking of?

On that general subject, are there generally-respected Mathematical 
style guides floating around somewhere?  I'm not necessarily looking for 
rigid codified rules, just for "accepted" (more or less) conventional 
ways of getting things done.  I'm relatively new to Mathematica but by 
no means new to programming and am fluent in many other languages (C, 
C++, Python, etc.), so I'm just looking for ideas about "the way things 
are done" that go well with Mathematica's functional nature but are 
still self-documenting as much as possible.


Erik Max Francis && max at &&
  San Jose, CA, USA && 37 18 N 121 57 W && AIM/Y!M/Skype erikmaxfrancis
   The chicken was the egg's idea of getting more eggs.
    -- Samuel Butler

  • Prev by Date: Washington DC Area Mathematica Special Interest Group
  • Next by Date: Re: Finding Clusters
  • Previous by thread: Re: Conventional way of doing "struct"-like things?
  • Next by thread: Re: Conventional way of doing "struct"-like things?