Average function in Haskell?

November 29, 2007

Can anyone show me how to write an average function in Haskell? Here’s what happens when I use (/), sum and length in the same expression:

Prelude> let l = [1, 2]
Prelude> sum l / length l

:1:8:
    Couldn't match `Integer' against `Int'
      Expected type: Integer
      Inferred type: Int
    In the application `length l'
    In the second argument of `(/)', namely `length l'

Writing an average function in pretty much all the language I know has been quite easy, but I still haven’t figured it out in Haskell.


The New Best Recipe

November 26, 2007

A few months ago, I decided that I needed a new hobby that did not involve a computer or a television. I quickly decided that cooking was going to be it: for a while I’d been wanting to learn to cook more than an egg or a grill cheese sandwich, it was going to be a hobby that would turn out to be quite practical (and also a good way to get girls) and in a way, it was sort of like programming: I’d take basic elements, combine them together and get a result.

The first step was to get a book; we have plenty of them at home, but they’re all recipe books, a bunch of recipes thrown together with a few images. Since I was going to start from the bottom of the ladder, I needed something that would describe the techniques, that would explain why the ingredients were chosen, etc. I needed a cook book, not a recipe book.

I knew that many friends in #grasshoppers were into cooking as well, so I asked them what books they recommended. jemfinch strongly suggested The New Best Recipe, a hard-cover book, 1000 pages long with over 1000 recipes for the low price of $35 CAD (at the time when CAD was still worth less than USD.)

So far, I have not been disappointed by The New Best Recipe; it’s exactly what I wanted. Before every recipe, there is a lengthy explanation of why the ingredients and cooking methods were chosen, the authors tell you what happened in other tests when they used alternative ingredients or different measures. My parents think it’s ridiculous that I patiently read the entire prelude to a recipe and the entire recipe before I begin, but I think that knowing why the ingredients were picked, what I’m going to be needing, the manipulations I’ll have to perform and an estimation of the time the recipe will take increases my chances of success.

Because of my work schedule, I don’t get to cook as often as I’d like during the week (I get home at around 5:15 PM, and dinner is almost always already cooking at that time), but I try to make it up during the weekends. So far, from TNBR, I made chicken piccata, spaghetti alla carbonara, pizza, chocolate chip cookies, pancakes, pound cakes and a few others. I’m hoping that I’ll get to try more during the Christmas vacations.

The book has some downsides however that I should mention. First, the table of contents is way too short, I would’ve preferred one with the list of all recipes, the index doesn’t indicate at the top what letter you’re at, so you need to flip back and forth to figure out where you are. I thought at first the absence of photos of the meal was a downside, but I don’t anymore: this would’ve made the book cost more money, and the explanation of what the recipe should be like is usually sufficient to get a good idea of what you’re shooting for.

I highly recommend TNBR to people who want to learn to cook, it’s a well invested $35.


Hiding source code

November 26, 2007

I visit a French programming forum, especially the functional programming and Python sections. A common recurring question in the Python section is how to use py2exe to create standalone programs. Most people who enquire about py2exe do it because they want something easy to deploy; Python is fun to develop, but it can be a bitch to deploy; no one wants to download the Python interpreter and all the necessary libraries required to run the program, it’s just more convenient to pack everything in a big blob file.

I said “most people”, that’s because the rest ask about py2exe to hide their code. Now, this is something I personally do not understand: why would you want to hide your source code? People who do open source software give out their code for a number of reasons. It could be because they want other people to contribute; it could be so that if they abandon the project, someone else can take over; it could be a philosophical thing, etc.

Giving out the source code in commercial applications is much less popular, but it happens. I don’t have names to give out, but I’m sure there are commercial applications developed in PHP or Perl or Python or in another language with an interpreted implementation. And even if the product is proprietary, the users of the software have the possibility to modify it. It could be forbidden in the EULA, but who reads and respects those anyway? To me, if you have the source code of a commercial application, you can modify it, but don’t expect support from them afterwards. It’s like the “void if seal is broken” sticker on Dell, IBM or HP computers; you can mess around inside the computer if you want, but you effectively voided your warranty.

Having the source code for an application is added value. A post on what it was like to work with a Lisp Machine explains that clearly: the programmer can fix bugs, add features without having to wait after the original developer (if he even agrees to add those changes.)

Some people would argue that hiding the code protects the intellectual property of the developer. Most software however, doesn’t seem to have anything intelligent nor proprietary in it. Most programs are an amalgam of well-known algorithms, document design patterns and API calls to popular libraries. Other people would argue that it improves security, because the evil hackers cannot read the code to find possible exploits. And we all know that no closed-source product has ever had an exploit discovered for it.<sarcasm>

No, I think the real reason people want to hide their source code is because they’re ashamed of their code. How could a company say that the developers they hire are the best when half the code in their product is featured on TheDailyWTF (sorry, I refuse to call the site by its new name)? Commercial code needs to have one feature: it must work. Management doesn’t care if it’s ugly, insecure, fragile, not DRY, etc. they just want something that works to sell to their clients. But what client would buy software that is badly programmed and that could cause lost productivity or even data loss? When you buy a car, if your see duct tape and loose wires under the hood, do you buy that car? Certainly not! Most programmers know their software is held together with duct tape, but as long as they can hide that fact from their clients, they can pretend it’s the best designed product on the market.


Looking for Final Fantasy VII for Playstation

November 15, 2007

Hello reader (readers?),

Lately, I’ve had an urge to play Final Fantasy VII again. We used to own it for the Playstation, and as far as I know, we never gave it away, but it appears my brothers “misplaced” it. A friend of mine lent me the PC version, however I find the control a little weird to use and I’d rather enjoy the game on my couch in front of my large TV.

Therefore, I’m asking people who read this blog, if you have a copy of FF7 for the Playstation that you could part from, I’d be very interested in buying it.

Thank you very much.


I ordered an XO-1

November 12, 2007

I’ve been without a laptop since my iBook G4 failed on me and that my AppleCare warranty ran out. Today, I decided to take part in the Give One, Get One program. For $400 ($385 CAD, HAH!), I bought two laptops, one for a child in a developing country and one for me. The machine has me very excited, it’s a very open platform (it even has a “view source” key!), Python plays a key role in the system, and the machine will allow me to do the small things I want to do with it such as web browsing, emailing and chatting.

I can’t wait to get it, I’ll report back when I’m holding this green computer in my hands.


The quality of French in Quebec

November 8, 2007

I want to talk about the state of French in Quebec. I would usually write this text in French, but the majority of my audience does not read French, and those that do know English too. I may also not want to make the mistakes I’m about to criticize ;)

An article published yesterday (Nov. 7th, 2007) in the French newspaper La Presse explored the quality of written French in high schools. The results were disastrous and alarming! The 40 works done by 20 students that were examined were ridden with errors: spelling mistakes, grammar errors, lack of punctuation, etc. Most of the words were written by how they sound, and some words were not even pronounced right! The word “couch” in French is “fauteuil”, but one student wrote it with a ‘d’ instead of a ‘t’! There is quite a difference between those two sounds. Another student wrote “gobe” instead of “job”. Yes, that student actually used an English word in a French work, and even though the word is just three letters long, the student managed to completely mess it up. It doesn’t even sound the same: “go” in French makes a hard G sound.

In a four sentence dictation, the average number of errors per student was seven; only three students out of 20 made less than five errors (and five errors in four sentences is still huge in my opinion.) One of the dictation had 20 errors, that’s one every two words!

Another related article revealed that on the 5th secondary (11th grade) ministerial French test, only 43% of the students in public schools passed the spelling and grammar section; in the private sector, that percentage was at 65%.

Those results are appalling. I don’t want to give the impression that I think I’m better than other people, but I take great care in writing correctly (ask my friends on #grasshoppers whom I bug constantly about English.) At work, people who need to have something checked quickly ask me. I was always very good in high school and in college, I rarely lost any points in essays because of my spelling or grammar errors.

The article is pretty harsh on students — with reason — but let’s not forget the adults. In my professional dealings, I receive quite a lot of emails, and the vast majority of them have the same kind of mistakes that the students made: spelling based on the sound of a word, the feminine and/or plural form are not applied to adjectives, punctuation is lacking. Hell, sometimes the messages are just plain unreadable, the sentences make no sense.

One of the worst thing about the whole the writing problem is how people can mess up with the simplest, most basic rules. In French, an adjective must agree with the gender and the number (singular or plural) of the noun it qualifies; they teach you that rule in the first grade. “Les fleurs sont jolies.” Jolies takes an ‘e’ because fleur is feminine, and an ’s’ because fleurs is plural. This is basic stuff, and I see people making this mistake all the time. People also struggle with using the infinitive form of a verb or the past participle since for a large number of verbs, they are homonyms. But there is a simple rule (mordre/mordu) that’s easy to apply and that can tell you with a 100% accuracy which tense to use.

When I hear Quebecois say that they want to protect their linguistic identity and what a beautiful language French is, and that I see them unable to master the simple and basic rules of the language they claim to love, I cannot help but wonder why they are so unwilling to make an effort to improve the quality of their written French. Maybe they just don’t care? In our digital age, writing correctly is absolutely essential: we write emails, SMS, IMs and others for a large portion of our communications. We send them to friends and family who may not care much if we make a mistake here or there, but we also conduct business with these media. It is a sign of care and professionalism to mind one’s quality of writing and a sign of sloppiness when a simple message is ridden with errors.

I don’t know exactly how to convince the other Quebecois to begin caring more about the quality of their written French, but I hope there is someone out there, more intelligent than me, who has a plan.