views:

201

answers:

8

Dear all;

I have a number of small algorithms that I would like to write up in a paper. They are relatively short, and concise. However, instead of writing them in pseudo-code (à la Cormen or even Knuth), I would like to write an algebraic representation of them (more linear and better LaTeX rendering) . However, I cannot find resources as to the best notation for this, if there is anything: e.g. how do I represent a loop? If? The addition of a tuple to a list?

Has any of you encountered this problem, and somehow solved it?

Thanks.

EDIT: Thanks, people. I think I did a poor job at phrasing the question. Here goes again, hoping I make it clearer: what is the common notation for talking about loops and if-then clauses in a mathematical notation? For instance, I can use $acc \leftarrow acc \cup \langle i,i+1 \rangle$ to represent the "add" method of a list.

A: 

Something like this website describes?

vicatcu
That was interesting, but not exactly. I'm looking at mathematical ways of expressing common programming constructs. See the examples above.
Dervin Thunk
+1  A: 

You might take a look at Haskell. Haskell formats well in latex, has a nice algebraic syntax, and you can even compile a latex file with Haskell in it, provided the code is wrapped in \begin{code} and \end{code}. See here: http://www.haskell.org/haskellwiki/Literate_programming. There are probably literate programming tools for other languages.

Kyle Butt
Haskell is awesome, but I have trouble reasoning about the performance of Haskell programs, due to laziness.
Jason Orendorff
+2  A: 

I'd copy Knuth. Few know how to communicate better than him in a computer science setting.

Peter
A: 

APL? The only problem is that few people can read it.

Mick Sharpe
+7  A: 

Don't do this. You are deviating from what people expect to see when they read a paper about algorithms. You should follow expected practices; your ideas are more likely to get the attention that they deserve. When in Rome, do as the Romans do.

Formatting code (or pseudocode as it may be) in a LaTeXed paper is very easy. See, for example, Formatting code in LaTeX.

Jason
Thanks. Yes, you're right. This paper is not, however, to be read by experts on algorithms, but by statisticians.
Dervin Thunk
Besides, I do need an answer to the question, rather than advice about what to do :(
Dervin Thunk
@Dervin Thunk: If it's for statisticians, they are far more likely to find pseudocode readable then some esoteric notation for representing algorithms in a compact format.
Jason
@Dervin Thunk: The number one rule of communication is knowing your audience.
Jason
@Jason: Again, sorry, but this is not the answer I'm looking for. You have to give me some credit. I've been writing all my life, for a living, I have a PhD and write papers all the time. If I ask a question, I usually expect an answer, not advice about how to do my job. That is why I'm not accepting your advice as answer.
Dervin Thunk
@Dervin Thunk: That's your prerogative.
Jason
+1  A: 

Lisp started out as a mathematical notation of a computing model so that the lecturer would have a better tool than turing machines. By accident, it turns out that it can be implemented in assembly - thus lisp, the programming language was born.

But I don't think this is really what you are looking for since the computing model that lisp describes doesn't have loops: recursion is used instead. The syntax derives from algebra where braces denote evaluate-this-and-substitute-the-result. Indeed, lisp's model of computing is basically substitution - what algebra essentially is.

Indeed, most functional languages like Lisp, Haskell and Erlang are derived from mathematics. Haskell is actually a result of proving that lambda calculus can be used to implement type systems. So Haskell, like Lisp was born out of pure mathematics. But again, the syntax is not what you would probably be used to.

You can certainly explain Lisp and Haskell syntax to mathematicians and they would treat it as a "game". Language constructs like loops, recursion and conditionals can be proven out of the rules of the game rather than blindly implemented like in other languages. This would lead you into the realms of combinatronics, another branch of mathematics. Indeed, in combinatronics, even the concept of numbers can be constructed out of the rules of the game rather than being a native part of the language (google Church Numerals).

So have a look at Lisp/Scheme, Erlang and Haskell if you want. Erlang especially has syntax close to what you want:

add(a,b) -> a + b

But my recommendation is to write in C-like pseudocode. It's sort of the lowest common denominator in programming languages. Has a syntax that is fairly easy to understand and clean. And the function syntax even derives from functions in mathematics. Remember f(x)?

As a plus, mathematicians are used to writing C, statisticians are used to writing C (though generally they prefer R), physicists are used to writing C, programmers are used to at least looking at C (I know a few who've never touched C).

Actually, scratch that. You mention that your target audience are statisticians. Write in R

slebetman
+2  A: 

I see if-expressions in mathematical notation fairly often. The usual thing for a loop is a recurrence relation, or equivalently, a function defined recursively.

Here's how the Ackermann function is defined on Wikipedia, for instance:

A(m, n) = n+1 if m=0; A(m-1, 1) if m>0 and n=0; and A(m-1, A(m, n-1)) if m>0 and n>0.

This picture is nice because it feels mathematical in flavor and yet you could clearly type it in almost exactly as written and have an implementation. It is not always possible to achieve that.

Other mathematical notations that correspond to loops include ∑-notation for summation and set-builder notation.

I hope this answers your question! But if your aim is to describe how something is done and have someone understand, I think it is probably a mistake to assume that mathematicians would prefer to see equations. I don't think they're interchangeable tools (despite Turing equivalence). If your algorithm involves mutable data structures, procedural code is probably going to be better than equations for explaining it.

Jason Orendorff
That's actually a nice syntax. I've been considering creating a language where functions are defined as `f(x)=expression` like in "proper" algebra.
slebetman
Thank you, yes, this is what I'm after.
Dervin Thunk
Why the downvote?
Jason Orendorff
+2  A: 

A symbol for general loops does not exist; usually you will use the summation operator. "if" is represented using implications, and to "add a tuple to a list" you would use union.

However, in general, a bit of verbosity is not necessarily a bad thing - sometimes, especially for complex algorithms, it is best to spell it out in plain English, using examples and diagrams. This is doubly-true for non-coders.

Think about it: when you read a math text-book on Euclid's algorithm for GCD, or the sieve of Eratosthenes, how is it written? Usually, the algorithm itself is in prose, while the proof of the algorithm is where the mathematical symbols lie.

BlueRaja - Danny Pflughoeft