Exercises in Programming Style–Style 1

NOTE : read the rest of the series, or check out the source code.



I was at Joy of Cod­ing ear­li­er this year and one of the high­light for me was Crista Lopes’ keynote  Exer­cis­es in Pro­gram­ming Style.

Crista demon­strat­ed how a sim­ple prob­lem of cal­cu­lat­ing term fre­quen­cy can be writ­ten in a pletho­ra of ways, includ­ing:

The point of these exer­cis­es is to allow you to see that there are many solu­tions to the same prob­lem, and that each comes with a set of con­straints that needs to be com­mu­ni­cat­ed.


I real­ly enjoyed the talk and bought Crista’s book so I can go through all 33 approach­es in my own time!


It’s tak­en me a lit­tle while to find the time to do this, but I was final­ly able to make a start last week­end. Over the next cou­ple of weeks I hope to share with you my ports of Crista’s Python solu­tions in F# and my take­aways.

If you like what you see then please buy the book and sup­port Crista’s work!


Style 1 – Good Old Times

This style was com­mon­place in ear­ly 50s when hard­ware lim­i­ta­tions had some hard con­straints.


  • Very small amount of pri­ma­ry mem­o­ry, typ­i­cal­ly orders of mag­ni­tude small­er than the data that needs to be processed/generated.
  • No iden­ti­fiers – i.e. no vari­able names or tagged mem­o­ry address­es. All we have is mem­o­ry that is address­able with num­bers.


Get the source code here.

As you can expect, not being able to use iden­ti­fiers real­ly stops you in your tracks and made an oth­er­wise sim­ple task so much more com­plex (also makes you appre­ci­ate the work of lan­guage and hard­ware design­ers that came before us).

The imple­men­ta­tion feels deeply unnat­ur­al due to the con­straints we have imposed on our­selves, and that’s kin­da the point of these exer­cis­es.

The absence of iden­ti­fiers for instance, forces us to lump a bunch of glob­al vari­ables into an array that acts as our ‘pri­ma­ry mem­o­ry’ for the pro­gram and forces you to remem­ber which index cor­re­sponds to what.

This style is real­ly at odds with how one would code in F# (prob­a­bly even more so than Python!), as by design F# nudges you towards code in a func­tion­al style — using immutable val­ues, recur­sion, using types to dri­ve your pro­gram, etc.