The Hyperpessimist

NOT WITTY ENOUGH!

Iozone3 for Raspbian

Lately I was thinking about SD card performance on the Raspberry Pi. The crazy long benchmarking thread complains about the duration to compile iozone3, so I went ahead and added it in my repository:

1
2
3
4
5
wget http://xivilization.net/~marek/raspbian/xivilization-raspbian.gpg.key -O - | sudo apt-key add -
sudo wget http://xivilization.net/~marek/raspbian/xivilization-raspbian.list -O /etc/apt/sources.list.d/xivilization-raspbian.list

sudo aptitude update
sudo aptitude install iozone3

(packaged the same as upstream Debian in the non-free section, which I just added to the repo)

Have fun benchmarking!

Go for Raspbian

I am a perfectionist. Therefore, after reading the instructions on how to install Go I was kinda annoyed: the Go release that is currently in Raspbian is 1.0.2 which is missing the improvements from Go 1.1. So I decided to create an APT repo with more current packages.

You have to run these two commands to add my repository and key to your system and then the other two commands to update and install the Go compiler.

1
2
3
4
5
wget http://xivilization.net/~marek/raspbian/xivilization-raspbian.gpg.key -O - | sudo apt-key add -
sudo wget http://xivilization.net/~marek/raspbian/xivilization-raspbian.list -O /etc/apt/sources.list.d/xivilization-raspbian.list

sudo aptitude update
sudo aptitude install golang

No compiling required, no Mercurial, no messing with $PATH, you have a go binary right in your path. Happy hacking.

This is my first ”proper” repository, which means it looks kinda like a proper Debian archive is supposed to look like and is signed, so you can trust these packages to come from me. It also offers source packages, so if you want to build your Go yourself you can do that as well. The repository was built with reprepro and while I am not yet completely familiar with the tool it seems to generate a valid APT repository.

Keep in mind that these packages are backports from Debian experimental and not affiliated with Debian, Raspbian and Go, just provided by me for your convenience.

Go Does Right Where D Goes Wrong

After not being too impressed with Go, I thought I’ll give D a shot. So I went to the university library and got myself “The D programming language”. The book is quite great but I haven’t been able to write any single line of D code.

Because it is neigh-impossible to actually use D. The DMD compiler is not free software, so this thing is out immediately. I don’t plan to use a language with only a proprietary compiler. LDC doesn’t seem to care much about ARM (see Raspberry Pi) and GDC missed inclusion for GCC yet again (and the GDC instructions for Raspbian are awful).

Contrast that with Go. After it got released, a gcc-go frontend got included into GCC 4.7. Also, the Go compiler can be built easily on a Raspberry Pi. I can install Go on my Arch Linux without problems as well as about any other relevant platform.

Whereas D struggles for years.

Ten Years of Python

When I check out the date I signed up at the Python-Forum, it says 20th of June, 2003. Whoa, that’s quite some time. Time for some retrospective, because Python got quite popular since then and maybe people are interested in how it was in the “early days”. Or well, earlier days.

The early days 2003-2007

Initially I started with Turbo Pascal and moved to Visual Basic 6. Visual Basic 6 used to have quite an awesome german-speaking community and since my english wasn’t that good some decade ago, I was doing my save game managers and stuff any high school student might do. Later I signed up for a programming course in school where we used Java. I got interested and bought Bruce Eckel’s Thinking in Java. If you check a footnote in the introduction, Bruce mentions Python. I remember this being the first time I heard about Python.

The first time I used Python was some time later, the polish edition of Chip ran a tutorial on Pygame. That was back then when the magazine was any good. I was quite impressed with how easy things were compared to the struggling I did with C++ (I still have one or two bad books on C++ on my shelf). Back then Python 2.2 was state of the art and I developed on Windows.

Needless to say that my game didn’t work out, I had no idea how to structure my code back then. The world missed out on a great Zelda clone with sprites borrowed from “Link’s Awakening”. Or not.

But I joined a web BBS and I learned a lot. At the beginning I was reading a lot of threads and answering questions. Usually this involved googling for answers, since the questions were often on the easy side. On the tricky topics, I read the responses of more experienced people in the BBS. I learned lots back then in very short time.

The Python landscape looked quite different back then. While Python 2.2 is certainly quite similar to the Python 2.7 we have today (minus decorators, list comprehensions were optional and generator expressions only came in 2.3), the ecosystem looked quite different. In 2003, nobody was speaking about the web very much. There was Zope and Plone but the programmers didn’t like it that much. Everybody else used CGI, which still exists today. Oh and there was a small fraction of Twisted users. Twisted, after all, invented the internet.

There was excitement in GUI frameworks, Qt 3 at that time and GTK+. GTK+ 2.x was in the process of obsoleting GTK+ 1.x and the binding required selecting which version to import. Much like GTK+ 3 is compatible to GTK+ 2, and Qt 4 is incompatible to Qt 3.

When Python 2.3 came out, many C modules were lacking Windows builds, so I started building stuff and publishing it and many people were happy. I’m not sure what the situation on Windows is, these days.

Community wise, there were many people complaining about significant whitespace in block indentation. As these discussions have mostly died down these days, I suppose that was uninformed bullshit by people who never tried. Also, dynamic typing was looked down upon from users holy-grail static typing languages like… Java. No interfaces, how could this possibly work?

The web years 2007-2010

I wouldn’t say 2007 is a hard break, as there was always some experimentation in using Python for the web. I, coming from PHP didn’t know better and experimented with Spyce which was a way to embed Python in HTML. There was also Karrigell which supported roughly the same thing as well as mod_python which could also run Python Server Pages (although it never really worked for me, I was expecting something like mod_php).

Then there was Webware, an application server. It was kind-of like the Java Enterprise Servers. Then the WSGI standard came along and Ian Bicking ported it to WSGI, dubbing it WSGIKit. Then it wasn’t good enough and Ian renamed it to Paste and after trying to explain it one, two, three, four times, probably everybody just moved on.

I think Paste was too ambitious at this time. Consider most people coming from a PHP background and WSGI went right over everybody’s head. Especially as Paste seemed to solve a problem that noone could see.

Roughly the same time, Colubrid surfaced, a WSGI helper library from Armin which made WSGI easier. Think of it as the spiritual predecessor of Werkzeug. If I remember correctly, Colubrid was used in Pocoo. Because in the beginning, Pocoo was meant to be a BBS to replace the phpBB used in the german Python forum. I think it died after the 0.1 release, possibly for being too ambitious and possibly for being too much ahead of its time. But Pocoo remained and became an umbrella organisation, kind of like Edgewall.

Hosting Python web code was terrible back then. I’d even say that classical PHP shared hosters never improved, they just got obsolete with the dedicated hosters that began surfacing.

Then Rails appeared. Let me rephrase: THEN RAILS APPEARED. If I were to pick one disruptive technology in the Python universe, I’d say Rails. After it got massively popular, a lot of Python frameworks inspired by Rails started appearing. And by a lot I mean a crazy lot: web.py (by Aaron Swartz), WebCore, Pylons, TurboGears, Grok (building on Zope 3), Repoze (kinda like Zope with WSGI), Paste, Aquarium, Nevmod Nevow, Snakelets, Django and a million others. At some point you saw a new framework every week and though “no, not yet another stupid ripoff”.

At the beginning, TurboGears looked like the most promising alternative, with a “best of breed”-approach on components. Unfortunately, Python didn’t really have decent components back then, so TurboGears was burning through template systems, ORMs and other small parts quite fast and the documentation was never up to date. Plus, it started having more and more dependencies.

On the other hand, Django seemed quite polished from the beginning. In the beginning there was no release so everybody ran the SVN version and even after they did releases, the pace of releases was too slow. But it worked, had great documentation and while not best of breed, it was Good Enough(TM). Plus, the strangest things were fixed before the 1.0 release (magic-removal, anyone?). The automatically generated admin was certainly the cherry on the pie and while many projects tried to mimick it, no decent alternative ever surfaced.

So yeah, we got Django. Good think that we converged on something.

The community years 2010-now

This is another semi-arbitrary division, but at some point, the web stuff was “solved”. These days, it is less about choosing the base technology but rather on integrating stuff and building things with Python.

I think we got vastly more Python programmers than a decade ago, using Python has become quite the no-brainer. A startup using Python these days? Hardly even worth mentioning. But the people using Python started to be more and more. When I started, there was a EuroPython (one of the organizers even asked me to do a talk but going to Sweden was not really financially possible for 16-year old me), but now PyCons appear in many countries. 2012 was the second PyCon DE and there were enough people attending to warrant another one. Also, there are Django conferences in Europe.

Apart from conferences, there are some user groups. I can only speak for Germany, where there are two or three larger user groups with regular meetings, but they are there and doing well. The Munich user group that started as a meeting of three people from the forum back in the Pocoo days still exists today and meetings are held every second week.

The position of Python has changed in the last 10 years. With Ubuntu making Python their lingua-franca (while Debian was more focussed on Perl) Python got some boost, but nowadays Python bindings are usually not just a bonus but more like a first-class feature. I can’t remember searching for some library and finding half-assed code or nothing at all. These days, software is written in Python from the get-go and if not there are bindings. The Raspberry Pi community has embraced Python. PyPI and setuptools/distribute have made it possible to reuse vast resources of code. Maybe not yet up to the variety of CPAN, but we’re getting there.

My personal involvement has lowered, though. In the beginning I rapidly became moderator and then co-administrator of the German forum (which also celebrated its 10th birthday 2012 on the PyCon DE with beers and obligatory cake), but studying has taken a lot of time and I often dabble in other technology where Python is not popular or even feasible.

Python on mobile phones is, well, “kinda” possible. Python in the browser is even less “kinda” possible. Systems programming is not a strong point of Python either. But don’t let that discourage you, for the rest it is hard to find any reason not to use Python. Maybe we are getting close to “nobody got fired for using Python”.

What changed? The indentation debates are basically dead and buried. The feasibility of using Python was demonstrated. Also, I believe many developers switched to Linux or Mac systems since 2003, so Windows (and the awful recompiling stuff) has lost importance. Or maybe the fact that Python 2.7 is out for a really long time now and everything is compiled for it has played its part. Or that platforms have lost their importance since web apps only need a browser and the backend system doesn’t matter much.

There is now a lot of resources on Python, free eBooks, paper books. Yet somehow more beginners manage to call the language “Phyton” or “Pyhton”. No idea why. Gotta be this terrible youth, when I was young…

Masterminds of Programming

After neglecting the book for some months due to finals season, I managed to finish “Masterminds of Programming” by O’Reilly. It is a somehow unusual book, since it consists entirely of interviews. I was sceptical at first, because interviewers usually pose the same set of stupid questions to everybody and see what happens but the authors fortunately went another way: it seems like they really prepared the questions that they wanted to pose each interviewee specifically after learning at least a bit on the topic.

The interviewees were by the way creators of several popular and also not-so popular languages. And the variety was quite astonishing. The funny part is, that from the interview you could judge the author pretty well in his technical views.

My favorite interviews where these that were surprising, that explained some historical quirks or context instead of just trying to plug the respective language. One of my favorite interviews was the one with Charles Geschke and John Warnock about PostScript, why PostScript is like it is. I also quite enjoyed reading Gosling about (not only Java) and he seems like a really smart person. Anders Hejlsberg of C# surprised me too, by not bashing on Java but instead on highlighting how to grow a language and how LINQ fits into C#. Then there’s the people who think their creation is the bees knees like Meyer’s Eiffel but does not convince very much. The interview with Larry Wall was also quite good, Larry Wall does not dabble too much in promoting Perl but he does not get lost in talking about blurb either.

The only interview that I ended up skipping was Robin Milner (of ML). This guy seems to be insanely smart but I have no idea what they were talking about in the firt part of the interview apart from not being very interesting for me. But the interviewers did a great job, they sounded like they understood Milner, so that’s a plus, I guess.

The biggest surprise was maybe UML. Jacobson raves on how UML is great and the second part of the interview is with Rumbaugh who seems to be desillusioned and sees the faults of UML and in I ended up agreeing a lot with him in just about every topic. Did not expect him to be so reflective and in the end kinda pessimistic about UML. Booch is somewhere in the middle ground.

And there is the interviews you can’t agree at all. Brad Cox of Objective-C was raving about SOA all the time. And in general, his observations were weird and I wouldn’t recommend taking his advices. What’s als funny: later in the book somebody else dismisses SOA as useless, which made me smile.

Overall, I can recommend the book. I got it from the university library, so I didn’t pay anything so the payoff was somewhere in the region of infinity, but well, the book is unique and the selection of topics is good. So if you have some spare time and money, get it.

The Things I Miss From Japan

Today The 7 Odd Things I’ve Missed About Japan was published and this prompted me to think back to my stay in Japan. I didn’t like many things, but I try to be fair and have to agree that I also liked a great deal of things.

1. Public toilets

Not so much the toilets themselves, I can do quite well without a heated toilet because in Germany bathrooms are usually warm enough. But I loved the fact that Japan had toilets accessible to the public everywhere. Every train station had a toilet, every combini had a toilet. And these toilets were free. And I mean free with a capital F, no black woman sitting there trying to guilt-trip me into paying for a toilet at a restaurant where I eat. No I mean really free - go there, use it, leave. For me free public toilets are a cornerstone of a great civilization. If we can manage to have free toilets everywhere, all other issues can be solved eventually :) In the end, I think combinis profit a lot from the free toilets, I was much more inclined to pay their markup for the product for all occasions where a combini saved be.

Well, I should also add that these toilets are clean. Or well, clean enough. German public toilets are most of the time totally awful.

2. Opening times

This only applies to shops not to banks or the dreaded ATM opening times, but here in Germany, especially in Bavaria shops close at 20:00 (22:00 in other states). And they are closed on Sundays. In Japan, I had no problem to go shopping on Sunday and I was aware of this good fortune so I went shopping on Sundays. And Aeon was open till 23:00, so go to a party, leave earlier, go shopping, go home. Super-convenient.

3. Combinis

I haven’t met anyone who doesn’t just love these. For the uninitiated: they are small supermarkets that are open 24/7 and usually have a rather great choice of products. Food is fresh, there is always a curious selection of softdrinks and a perhaps even more curious selection of alcohol. You can get your Amazon packages delivered there or pay your Amazon stuff there as well as order tickets. They also offer fast-food as well as food that you can prepare with a microwave or water cooker. I think I ate countless breakfasts at combinis, sometimes because I was out camping and sometimes because I didn’t feel like making food.

In the end, you pay maybe 30% markup on the products, but in my experience it is usually even less. I think this is very good deal.

Visiting a combini at night with a bunch of friends to buy some beers also became a cool ritual. That is the one occasion where you don’t mind the ridiculously high beer prices.

4. Trains

Oh how I loved the subway system in Nagoya. It is a number of lines crossing through the city and one ring line that enables you to reach every place in a decent time. In munich, to go anywhere, you have to go into the city and then ride out again. This takes ridiculous amounts of time and is incredibly inefficient.

And Japanese trains are never late. Never. There is this joke: “In Heaven: the cooks are French, the policemen are English, the mechanics are German, the lovers are Italian and the bankers are Swiss. In Hell: In Hell: the cooks are English, the policemen are German, the mechanics are French, the lovers are Swiss and the bankers are Italian.”. No idea what the Japanese would be in Hell, but in Heaven they would certainly run the trains. I suppose, they would also do a decent job of running trains in Hell.

5. The Logistics

I could set the delivery date and also the desired time. Back in Germany I have to be happy if the delivery-man even bothers ringing our door. For me it is quite ok still, but if you live in a dorm, too bad because getting a package delivered to a dorm is near impossible.

Added bonus: Amazon delivers just about everything for free, without the need for Amazon Prime.

6. Cheap Sushi!

I had a friend from Japan over and she was surprised how horribly expensive sushi in Munich is. And yeah it is, because everything Japanese in Munich is automagically 20% more expensive. No more cheap “50% off” sashimi from Aeon, sigh. Of course, expensive sushi is probably more expensive in Japan, but cheap sushi is cheaper in Japan, too (and also pretty tasty!).

7. The all-you-can-eat-sweets

Haha, I’m a glutton. Sweets Paradise offers a deal where you can eat as much cake and sweets as you can for 1480 Yen (about 14,80€) for 90 minutes. Turns out, 90 minutes are enough to eat enough sweets until you won’t be able to eat anything sweet for the next two or three months.

Seven Languages, Week 7: Haskell

So, this evening I finished the homework of Haskell day 3. Immediately after that I went to grab a beer as a small token of celebration for finishing “Seven Languages in Seven Weeks” successfully (I’d say).

What about Haskell? Well, I like it. Programming in a functional way in Haskell is actually quite fun and straightforward. As for functional languages Haskell is at the top and actually quite what I’d wish Scala to be. From the languages I kinda sorta “speak” Haskell compares best to OCaml and in a number of things favorably.

In specific, I really like the type classes and found working with them extremely pleasant. Also, in some cases the Haskell syntax is less messy, especially when defining types, I never liked the alpha, beta, etc stuff that OCaml definitons include. Other things I liked less, like the indentation. Not exectly because of the indentation itself, but the ugly results:

1
2
3
exploreMaze p = let complete = completePaths p
                    found = complete /= []
                    in if found then (head complete) else exploreMaze (p >>= extendPath)

So, I get punished for giving long names. Possibly this can be circumvented, but as I have quite a lot of stuff to do than learning Haskell properly, let me complain about this.

Another thing was that right in the first homework I ran into problems with the monomorphism restriction, so I had to add a line to the top of my code which disabled the restriction. I don’t really know the exact reasons why it happened but I fear the explanation is not simple. But that leads me to some things I really liked about Haskell.

For once, the REPL of GHC, GHCi is pretty awesome. I’d say it is by far the best from the other ones in the book. It supports readline directly, it has tab-completion for files, starts quite fast and works (almost) like if I were writing code in a file (well, I have to use let to set values, but this is shorter than Prologs assert). The error messages of GHC are usually really cool and sometimes even offer the proper name of the function if I mistype function names. Awesome.

There is a number of handy things that I liked, like the . compose operator and even more the $ application operator. Not entirely surprising since my favorite operator is the thrush-operator -> (sometimes called thread-first operator, common in Clojure), so yeah, I like control-flow-bending operators.

Talking about control-flow, let’s talk about monads. Haha, of course, how can one write a discussion of Haskell without monads? Well, I’ll be smart and won’t write a tutorial on monads just yet, but if you haven’t seen monads before, like me, and feared to learn about them because they were deemed “complicated” rest assured they are not. After I saw my first monad, I was more like “that’s all there is to it?”. For once, the “Seven Languages in Seven Weeks” description was quite good and I picked them up quickly.

The book introduces for the Maybe monad which is quite cool, since it solves exactly a problem that I’ll run into in my bachelor thesis in OCaml soon: propagating faulty state without having to pass the fact that something failed everywhere. After I saw the Maybe monad, I thought it would be cool to have a version of it that can not only pass around the fact that something failed but also what exactly. Turns out, such a thing exists and is called the Error monad. So yeah, the trip to Haskell-land has paid off already.

Generally, I quite liked Haskell and even learned some things on the way. If I were to judge, I’d say that the Haskell chapter has learned me more than the other chapters from the book, which is maybe not that surprising since the topics discussed in the Haskell chapter were the most in-depth ones.

Seven Languages, Week 6: Clojure

We’re near the end and this week (cough, the last 2 days) I did CLojure. I used Clojure some years before, I even got “Programming Clojure” standing in my shelf, although I with it were “The Joy of Clojure” instead. Nevermind.

Clojure is powerful. Actually, Clojure is immensely powerful and super-concise. My experience is whenever I ask for code review on the Clojure IRC channel or mailing list, they reduce my code to 1/3 of it’s length. Form the book, it was obvious that Bruce Tate liked Clojure as well, unlike e.g. Scala, so in the Clojure chapter he also described what was not covered in depth.

Personally, I could have gone without defrecord and defprotocol, as the introduction was kinda weak and did not make much sense. The usage of protocols felt incredibly forced and the homework where one should design and implement a protocol was difficult, because I had no idea what to implement at all. So I made a protocol for ranting.

But this time, the homework was at least not that boring, in general. For example, one was to implement unless, so I actually went the extra mile and made it work just like if which means that it can take an optional else clause but does not have to.

1
2
3
(defmacro unless
  ([test iffalse] (list 'unless test iffalse 'nil))
  ([test iffalse iftrue] (list 'if (list 'not test) iffalse iftrue)))

So the first version expands into a recursive macro call with the second argument set to nil, in a similar way like if works. Coming from Scheme and syntax-rules I think this looks kinda ugly to construct these things with literal calls to list and manual quoting. Well, I guess it is simpler and simplicity is a big proint for Clojure. And furthermore I can keep my syntax-rules if I like.

Day 3 had an interesting/tricky task, the Sleeping barber problem, where one had to implement a concurrent solution using the Clojure primitives. Turned out to be tricky, because I’m not really sold on agents. Implementing this problem with Actors would have been easy/trivial, just send some messages around. With agent, you do it the other way ‘round: instead of sending messages to functions, you send functions to threadsafe values.

Changing my thinking took quite forever, but the first step to get to a solution was to define an agent. The first breakthrough was that barber was an agent of the number of people he serviced. So you could implement a function that pauses for 20ms (“working”) and then increments the counter. So, maybe it would make sense to make the number of free waiting spots an actor too? I did this and implemented a function to try to add a customer to the waiting queue (try-occupy) and turn him down if it is full already. Freeing a seat is easy (when the barber finishes a customer and gets a new one), just increment the number of free seats in free-seat.

Now that you have a function to free-seat, you need to tell the barber to free one seat before going off to work on the customer in cut-hair.

If you run it on this point, nothing happens, because no customers arrive. This kinda ticked me off because now we’d need to create a thread that tries to occupy a seat every 10 to 30ms and this did not seem elegant. Yet at the end, I did not see a better solution so I helped myself to a function that sleeps and recursively creates new customers, client-arrive.

When you try starting it at this point, you realize your waiting queue is full with 3 customers but the barber does nothing. So yet again, we need a function that moves people from the waiting queue to the barbers chair, move-to-chair.

Then just start these two threads, wait for 10 seconds and check the value of barber. For me, in the initial run on Clojure 1.4.0 it was around 250 clients serviced which was rather poor, with Clojure 1.2.1 it was three-hundred-something which was better but still poor. I considered running it on a faster computer and it dropped to 122 customers which was really awful.

After some head-scratching I tried putting the move-to-chair thread to sleep for 1ms, to prevent it from gobbling up all computation. Lo and behold, 480-490 customers out of a theoretical maximum of 500, I’d say this is pretty decent.

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
; barber <serviced>
(def barber (agent 0))
; seats <free>
(def free-seats (atom 3))

(defn any-seat-occupied? []
  (< @free-seats 3))

(defn free-seat [free]
  (inc free))

(defn try-occupy [free]
  (if (= free 0) 0 (dec free)))

(defn cut-hair [serviced]
  (if (any-seat-occupied?)
    (do
      (swap! free-seats free-seat)
      (Thread/sleep 20)
      (inc serviced))
    serviced))

(def continue-running (atom true))

(defn move-to-chair []
  (loop []
    (if @continue-running
      (do
        ; prevent this thread from gobbling up all processing power
        (Thread/sleep 1)
        (send barber cut-hair)
        (recur)))))

(defn client-arrive []
  (loop []
    (if @continue-running
      (let [time (+ 10 (rand-int 20))]
        (Thread/sleep time)
        (swap! free-seats try-occupy)
        (recur)))))

(future (move-to-chair))
(future (client-arrive))
(Thread/sleep (* 10 1000))
(println @barber)
(reset! continue-running false)
(shutdown-agents)

To my great surprise, I had absolutely no correctness problems at the first try. Maybe it paid off that I was drawing the things on a piece of paper to compose my thoughts?

I was curious how other people solved this and to some point I was actually quite disappointed by some solutions. Ben’s takes the cake, because he writes Clojure with Java syntax which makes my eyes bleed (actually, he writes Io like this too) and his solution is verbose and awkward. This awkward-syntax thing is also in Bash-Shell’s code (seriously, camelCase in Lisp‽) but the actual solution behind it it is similar to mine. Devnoo again is awfully Java-istic. But there is hope, Yannick’s solution seems to me quite idiomatic and Frédéric’s solution is so immensely similar to mine, I was actually wondering. Actually, after I wrote mine, I took some inspiration from him, like the fact that free-sets should better be an atom and not an agent as well as to use future instead of the (.start (Thread. function)) clutch I had before.

Seven Languages, Week 5: Erlang

So, Erlang this week. Well, I have to admit that I understand why people think Erlang is wonky. It seems like they took the odd syntax of a logical programming language (hint, hint, hint) and made a general purpose language. Oh wait, they did.

I have to admit that I kinda liked the concepts, the pattern matching on tuples, the restarting of processes. This all feels simple and powerful. And to my surprise the BEAM VM does not use that much memory as I was expecting. Things were instataneous on my old trusty ThinkPad. Much appreciated.

Due to tail call optimization, loops with pattern matching are actually pretty elegant, you just call the function again and that’s it. The syntax for transmitting messages is straightforward, no wonder Scala was inspired by this.

But there’s a number of strange things: the exact syntax of delimiters is driving me up the wall. Like in Prolog you can define a predicate with pattern matching, but in Erlang definitions of a function must be consecutive and be separated by semicolons and finished with a full stop. This example:

1
2
3
4
5
words("") -> 0;
words([32]) -> 0;
words([_]) -> 1;
words([32|Tail]) -> 1 + words(Tail);
words([_|Tail]) -> words(Tail).

Every time I had syntax errors in Erlang, these were caused because the interpunctuation was wrong. Because it is non-intuitive and random. Oh well. From the example, you can see another wonky thing: when you put numbers in a list, Erlang sees this as a string. So this example calculates the number of words in a string separated by spaces (ASCII 32). It is a kinda odd way.

The book chapter on Erlang was quite ok I’d say, just day 3 homework was so frustrating that I left it out. Let me tell you how the tasks looked like:

  • Implement a task that monitors another and restarts it if it dies
  • Implement a task that monitors itself and restarts itself if it dies
  • Implement a monitor that monitors the monitor and restarts itself if it dies

Well great, basically the same thing three times, so I just left out this homework. The remaining tasks were surprisingly fun.

Back to Erlang: I can’t see myself using Erlang because of these syntactic oddities, but I liked the concepts. Maybe something like Lisp Flavored Erlang or perhaps better Elixir would fit my needs better. Actually the latter seems to be in the spirit of CoffeeScript to Erlang, so I’d probably try this first.

Seven Languages, Week 4: Scala

After the slowness caused by Prolog, I managed to finish Scala almost on time this week. I’ve used Scala before, to build an Android widget and a compiler for Caml Light, but I can’t really say I like the language too much.

On the one hand, it sounds like a dream language: a powerful functional programming language with strong support for functional programming while retaining OOP support. That sounds pragmatic. Unfortunately, it isn’t: the type system in more complex than in languages like OCaml and the syntax is rather unpleasant for me.

What’s funny is how the “Seven Languages in Seven Weeks”-book basically picks out the parts of the language that a friend of mine considers warts. First, the possibility of adding strings together via +, second the Actors which seem to be outdated due to Akka and third, XML literals are an awful idea. Kind of “good on paper, bad in reality” awful, like checked exceptions in Java.

On the other hand, I kinda liked the homework, the size of it was fitting and I had fun exchanging strategies to solve it with a friend. For the censor homework I decided to use a simple string replace but add captitalized variants and read the censor words from a JSON file, whereas my friend used a more efficient solution with regular expressions and a custom text format. Both are valid and it was fun to compare.

Also: the new profile pages on GitHub with the contribution graph are fascinating and awful, because they add a certain amount of game to it (gamification): I have to contribute, otherwise my current streak breaks. And the longer the streak, the more geek-cred. Or something like that. Unfortunately, it also causes more useless commits to prevent streak-breaking.

After all, if I were looking for a JVM language to use on Android phones, Scala would probably be it.