Peter Miller

in

Pen & Paper Programming

With two monitors at home, I find myself using less and less paper while programming. Whenever possible, instead of jotting down notes on a piece of scrap paper, I use my second screen. Not only do I save paper, it makes it automatic to save my notes in for later use. In general, it just feels right to be working mostly in electronic formats.

However, there is always still a place at my desk for a pen and pad of paper for those tasks which still just don't translate as well to the screen. One such task is code simulation, familiar to anyone who's taken an introduction to programming class. The idea is to take a piece of code, often some kind of looping or recursive structure and explicitly spell out exactly what would happen given a certain input. By doing this you can reveal obscured bugs or just plain figure out how the code works.

Typically this technique is used in introductory courses, because as you learn more about a language and programming, you tend to internalize what was a pen and paper process into a wholly mental, almost unconscious one.

I have not been taking any introduction to programming courses, but I have been reading Programming Erlang, a new book from the Pragmatic Programmers by one of the designers of the Erlang language, Joe Armstrong.

Erlang is a functional language, designed specifically for distributed, fault tolerant systems. Originally it was used in Ericsson's telecommunication switches. So if you, like me, are not that familiar with functional languages, Erlang can seem pretty strange at first.

I was getting over the strangeness of it all until Armstrong presented a bit of code that takes a list of returns a list of all its permutations. Up to this point he has explained every code snippet in detail, but for this piece of code he tells the reader to figure it out on the reader's own. And this situation brings us back to code simulation, and my pen and paper, ready for a worthy challenge.

I struggled with how to present this since I can't assume anyone reading this has any level of familiarity with Erlang. I decided to go with the full immersion approach; please check out Armstrong's book if your interest is peaked.

So, here's what the permutations function was supposed to do. The following is an example session within the Erlang interpreter:

1> c(perms2).
{ok,perms2}
2> MyList = [ 1,2,3 ].
[1,2,3]
3> perms2:
module_info/0 module_info/1 perms/1
3> perms2:perms(MyList).
[[1,2,3],[1,3,2],[2,1,3],[2,3,1],[3,1,2],[3,2,1]]

Like I said full immersion; the lines with the number> are user input, the lines below are output from Erlang. Line 1> is me telling Erlang to compile the code in permsModule.erl and import this code into my interpreter session. Line 2> is me creating a list with the elements 1, 2 and 3. Line 3> is me invoking the perms function from the imported code (in a kind of namespace.function syntax) with the list I created as an argument. Hopefully the resulting output makes sense, it is a list of lists, each list within the greater list being a permutation of the original list.

So far so good? Without further ado, the Erlang code for the perms function: 

   1: -module(permsModule).
   2: -export([perms/1]).
   3:  
   4: perms([]) -> [ [] ];

5: perms(List) ->

[ [Head | Tail] || Head <- List, Tail <- perms(List--[Head]) ].

Line 1 tells the Erlang compiler that the code in this file is part of a module called permsModule. Line 2 says that when someone else imports this module they can use a function called perms which takes one argument (sadly this is not as exciting as the idea of figuring out what the function perms divided by one is supposed to mean). Line 3 is a blank line I put in to make the code look nicer.

With 60% of the code now covered, my work here is over halfway complete right? I think it is time for a break. I'll finish this code off next time, but before I go, here's some more sample output from that previous Erlang interpreter session for you to ponder:

4> EmptyList = [].              
[]
5> permsModule:perms(EmptyList).
[[]]
6> [ [] ].
[[]]
7> ListOfOne = [1].
[1]
8> permsModule:perms(ListOfOne).
[[1]]
9> [ [1 | [] ] ].
[[1]]
Posted: Oct 10 2007, 09:56 PM by pmiller | with 2 comment(s)
Filed under:

Comments

Peter Miller said:

We left off last time&amp;nbsp;having covered the first 3 lines of the following Erlang function to calculate...
# October 14, 2007 4:28 PM

Desenvolvimento de Jogos: Logica De Programacao said:

Pingback from  Desenvolvimento de Jogos: Logica De Programacao

# March 29, 2009 9:43 PM
Leave a Comment

(required) 

(required) 

(optional)

(required)