Want to read Slashdot from your mobile device? Point it at m.slashdot.org and keep reading!

 



Forgot your password?
typodupeerror
×
User Journal

Journal Alioth's Journal: Adventures in Functional Programming, part 1 6

And now for something completely different.

For years, just like pretty much anyone else who writes software, I've been using imperative languages. All the days from ZX BASIC, BBC BASIC, Z80 assembler, 6502 asm, C, C++, Perl, php and Java. (And even some COBOL, but I don't like admitting to that). Most recently, most of what I do is in Perl, or shell script, or Java.

That's a long time habit of doing things like "i++".

At times, out of disaster comes opportunity, Recently, our largest customer went into administration (for US readers, I'm not sure how this maps to Chapter 11 or Chapter 7, but basically it means the company is insolvent, and its management has been taken on by a third party who will try to sell what's left). This has left me with quite a lot of time to spare because a project I would have been working on right now is currently on hold until the situation with our customer is resolved (and the outcome looks like it should be favourable).

I've heard bits and pieces about functional programming, and Haskell, and Erlang - but never really had looked into it. My memory was jogged on Erlang when I was reading an article about telephone exchanges, somewhat coincident with the above catastrophe with our customer. Erlang was initially developed for telecommunications, where five 9s are simply not good enough, and I'm always interested in something that can help me make more reliable systems. I'd rather be developing than cleaning up after a system crash. So the time was ripe to start on Erlang.

What makes me interested in this language is that it is a tool that's designed for making reliable, concurrent systems. It has good features for that such as extremely light weight processes, the VM supports upgrading software *WITHOUT* having to shut down the program - you can upgrade/fix a service that's running and NOT shut it down to load the changes. It is also a functional language. And functional languages to me are (at least until last week) an alien concept.

The journey has only just begun. I started with "Learn You Some Erlang for Great Good" (it's titled in Engrish, but written in English) - http://learnyousomeerlang.com/ . I now also have the O'Reilly Erlang book, which I'm just about half way through reading and working through the examples.

The striking thing is you have to think a bit differently for a functional language like Erlang. You don't so much as describe how a task should be completed, but declare What The Truth Is - in a way that is very similar to how you write functions in mathematics (indeed, one of the first examples in Learn You Some... is a function that returns the factorial of a number, and the Erlang has a direct correspondence with the way a mathematician would write a factorial function). The way lists can be manipulated (list comprehensions) is straight out of mathematics set theory. Although the language looks strange and impenetrable and ugly at first, soon you see a mathematical beauty emerging. The other striking thing is that variables aren't. (I'd have called them "invariants" - calling them variables isn't all that accurate since they can't be varied once assigned). But you don't need variable variables in a functional language.

The other striking thing is how you must think differently with Erlang if you're making a concurrent system. The O'Reilly book points out that they've encountered time and time again, when people from an imperative background start using Erlang, they do it wrong - they write the system like they would in, say, Java, so with a minimal level of concurrency. But processes in Erlang are really, really cheap so you've got to get rid of all those ideas on how to write a large system. For example, the book cites how one group had set out to make an IM proxy in Erlang. They had one process handling the sockets from clients, another process doing some work on the data, and a final process emitting the data to the downstream servers. They ran into trouble with more than half a dozen concurrent messages. But how they should have designed it is to create a new process for every single packet - processes are *that* lightweight that you can make tens of thousands of them quickly and without huge memory overhead - and their proxy could handle orders of magnitude more simultaneous messages. Interprocess communication is also very lightweight, and lacks the shared memory drawbacks of traditional threads. Essentially, in Java or C++ or C# you think like the Protoss - a few heavy weight warriors. If you write Erlang, you have to think like the Zerg. A veritable swarm of lightweight processes, and zergling rush your problems :-)

This neatly segues onto something I did many moons ago in university. We learned a design methodology, written by a man called Michael Jackson (not the dead singer, the still alive computer scientist). JSD (Jackson Structured Development) and JSP (Jackson Structured Programming) were methodologies developed in the 60s and 70s, and today are largely forgotten, in fact in the 90s when we were learning this stuff there was much grousing about it being "obsolete"... but one thing that keeps coming back was when analyzing your problem, you analyzed it as if you had the perfect concurrent system and so you modelled every small problem as a completely new process. In those days you'd eventually flatten it down to effectively a single thread. I remember remarking to a classmate at the time that there seemed to be an elegance of starting your model as if you had the ideal concurrent system because it made it clear - and that perhaps one day we would have a system in which you could take the design and implement it as a concurrent system, just as you had drawn it. Perhaps it's time that JSD was revisited, because in Erlang you DO make every single little process that happens in your inherently concurrent world an actual Erlang process, just like Jackson suggested you model it! JSP, which we used in COBOL at the time, and COBOL itself was the source of much grousing - (JSP is specifically good at program design when you have an input, you transform it, and make an output. The stuff of many COBOL programs. If you model your inputs and outputs correctly, the resulting program structure makes programming a simple mechanical task and you get no logical errors in your code) - it strikes me that JSP fits amazingly perfectly with XSLT. Indeed, JSP models everything as a tree. Now what's XML? Basically, your program structure is exactly how XSLT is structured - a tree. So is your input and output.

So perhaps Michael Jackson really is a genius, too.

This discussion has been archived. No new comments can be posted.

Adventures in Functional Programming, part 1

Comments Filter:
  • all worth it just for this, "Essentially, in Java or C++ or C# you think like the Protoss - a few heavy weight warriors. If you write Erlang, you have to think like the Zerg. A veritable swarm of lightweight processes, and zergling rush your problems :-)"

    I have been reading a bit on administration in the press due to the Portsmouth situation [bbc.co.uk]. Made me feel good that I immediately knew what you were talking about.

    I've been trying to get my head around the ideas of functional programming, but

    • by Alioth ( 221270 )

      I don't have a strong mathematical background either - at least in terms of what most people think as "hard mathematics" (i.e. stuff like differential calculus) *but* I think what helps is that at university, one of the parts of our course was Discrete Mathematics - which has a lot about set theory and mathematical modeling, and also included things like formal specification...and Erlang is making it all come back. (One thing I noticed were the people who'd done lots of what I call "hard maths" - i.e. all t

      • I never got that far. I kick myself over it now because finding the time to catch up is near impossible and I don't really "need" it. I got as far as trig, and when I took that the class was so poorly taught that I managed to get an A without actually learning anything. My wife has a math degree and was livid. She still gets mad if I bring it up.

  • by Tet ( 2721 ) *
    The thing about Erlang is, much there is to like about it, there are too many things I don't like for me to get on with it for everyday use. The io:format() syntax is horrendous in a world where printf() syntax is ubiquitous (and already was at the time Erlang was designed). String handling just plain sucks. Immutable variables make for awkward code:

    Var1 = some_function(),
    Var2 = some_other_function(Var1),
    Var3 = a_third_function(Var2).

    OK, so that's a contrived example, and in this case could be rewrit

    • by Alioth ( 221270 )

      The "Learn you some Erlang..." tutorial also notes the issues with string handling, and has sections every so often "Don't drink too much kool-aid" - with examples of where perhaps Erlang wouldn't be best. (I liked the tutorial's honesty - it's not trying to preach that Erlang is the be-all and end-all).

      However, to be fair, all languages have their ugliness somewhere - but knowing in the past only imperative languages, it's possible that I may have been blind to these uglinesses (It's Just The Way You Do It

  • Just FYI (I have unfortunate experience of watching a former employer go under, who had to dissolve a small US sales offshoot as well as the main UK company); USA chapter 11 is administration in our law, USA chapter 7 is liquidation or winding-up.

If all else fails, lower your standards.

Working...