Filed under: misc — jlm @ 16:25

I hate in when I look up foo in the dictionary and it says “not bar”. Thanks, now I’ve gone from having to look up a word to know what it means, to having to look up another word to know what the first means, you’ve been a big help there.


Haskell vs. C readability stawman

Filed under: programming — jlm @ 17:03

Every time I think about learning Haskell, I head over to haskell.org, read the Introduction link, and am offended by the astoundingly absurd quicksort example they provide for its superiority over C and leave in disgust.

First, let’s take a look at their Haskell implementation of quicksort:

    qsort (x:xs) = qsort (filter (< x) xs) ++ [x] ++ qsort (filter (>= x) xs)

Wait one second here! Something’s fishy. At the bottom, the basic operation quicksort does is a swap. Where’re the swaps? I don’t see any swapping. I do see splicing though; quicksort doesn’t splice. This isn’t quicksort; quicksort is an in-place sort, this thing here is a list sort. This is not just an implementation detail, it’s fundamental to what quicksort is. It doesn’t splice, it makes swaps. If you have some linked lists, you sort them with something like mergesort. But for arrays, you can’t beat quicksort.

Now, we can start on the C implementation they provide…

    qsort( a, lo, hi ) int a[], hi, lo;


If you’re a C programmer, your first impression is to boggle at this syntax. It’s been deprecated for 17 years, and most C programmers nowadays probably don’t even recognize it. What moth-eaten tome did they dig this out of? Variables of hi and lo and h and l? l+1 and l-1 as expressions? That’s the kind of thing you see at the IOCCC. Let’s choose an actual decent example of quicksort for a more honest comparison, like the one from K&R2:

/* qsort:  sort v[left]...v[right] into increasing order */
void qsort(int v[], int left, int right)
    int i, last;
    void swap(int v[], int i, int j);

    if (left >= right)    /* do nothing if array contains */
        return;           /* fewer than two elements */
    swap(v, left, (left + right)/2); /* move partition elem */
    last = left;                     /* to v[0] */
    for (i = left+1; i <= right; i++)   /* partition */
        if (v[i] < v[left])
            swap(v, ++last, i);
    swap(v, left, last);        /* restore partition elem */
    qsort(v, left, last-1);
    qsort(v, last+1, right);

Much more readable than that strawman example they have on the Haskell intro page!
But it’s still an apples-to-oranges comparison, because the C implementation is doing a real quicksort, and the Haskell is doing a list sort. Show me a real in-place quicksort in Haskell: It’ll turn out looking almost like the implementation in K&R, I wager.

Now, Haskell was written by people who know their computer science. They know all about quicksort, they know their example is comparing different algorithms, and they can write better C code than what they use. Why do they have to be dishonest when promoting the virtues of Haskell, hmm?


Latest thoughts

Filed under: philosophy — jlm @ 13:37

Why is it that things we find very easy to do in our minds (process language, identify people, etc.) are things we can’t get machines to do, but things we find very cumbersome (arithmetic, data manipulation, etc.) we can make machines to do extremely well?

This dilemma, that stuff we can do easily doesn’t imply easy automatability, leads to all kinds of optimism on how human-like computers and other machines are. So what’s the root of it? I think it’s because we don’t know how to understand language, identify faces, etc. We do it, but we don’t know how! For all of recorded history, we’ve been refining mathematics, improving manufacturing processes, so we know in fine detail how to do arithmetic, how to weave cloth, etc. and we teach it to the next generation, write it down in books — whereas we don’t teach our children how to understand language, how to identify their parents and friends, these are mysteries that our brains do for us without being taught, processes which we have no visibility into the internals of. We can make a step by step guide to how to multiply numbers, and make a machine to do it. But we don’t have step-by-step guides to language. The steps are inside our brains and we get only the output. We don’t understand how we understand language, and so find it impossible to make a machine that does it.

Update: For the ~0 of you who’ll read this, I’ve just learned that this is called Moravec’s paradox.


Pasadena Chalk Festival

Filed under: so. cal — jlm @ 09:01

Pasadena had its chalk art festival this last weekend, so what better way to give my brand new camera (a Canon PowerShot A540) a spin?
Photos here. The last photos taken by my old camera (an Olympus D-340L) in Turkey before it died are here.


I’m back

Filed under: travel — jlm @ 11:19

I’m back home. I’ve written a lot into a travel journal, I’ll be putting it up online soon.


Türkiye’ye gitim.

Filed under: travel — jlm @ 14:12

(“I’m off to Turkey.”)

[Flag of Turkey]

Report once I get back.


Dvorak HOWTO

Filed under: general — jlm @ 18:37

[Dvorak key layout]

No, it won’t make you type faster (or probably not by much… top speed typists all use Dvorak, but I didn’t notice any improvement) or more “cheaply”, but it is easier on your hands, and reducing finger fatigue makes it a clear winner in my book.

How to switch:
Windows NT:
    Start ➙ Settings ➙ Control Panel ➙ Keyboard ➙ Input Locales ➙ Properties ➙ Keyboard Layout ➙ US-Dvorak
    Hit “OK”, then “Apply”.

Windows 9x:
    Same, but you’ll probably need to insert the install CD when it says, as the layout isn’t part of the default install, so have it ready.

Windows XP:
    Start ➙ Control Panel ➙ Regional and Language Options ➙ Languages ➙ Details ➙ Settings ➙ Default Input Language ➙ United States-Dvorak
    If it’s not availabe, under “Settings” go to Installed Services ➙ Keyboard ➙ Add and select it, then you’ll be able to set it as the default.

Linux console:
    “loadkeys dvorak” will change the current keymap to Dvorak. (Or sometimes “loadkeys dvorak/dvorak”, look around /lib/kbd/keymaps.) Making it the default usually involves setting the “KEYTABLE” attribute in /etc/sysconfig/keyboard to whatever you used with loadkeys.

X11 on PCs:
    xmodmap pentdvor.xmod

X11 on Sun 5x keyboards:
    xmodmap sun5dvor.xmod

Other X11:
    First, run xmodmap -pke > qwerty.xmod so you have the current keymap safely saved in case things get messed up. You’ll want to copy “xmodmap qwerty.xmod” into a cut buffer so you can paste it with only the mouse.
    Run xmodmap q2d.xmod
    This maps the Qwerty keysyms to Dvorak, and can fail sometimes, like if two keycodes generate one of the moved keysyms. Because it operates on keysyms, not keycodes, this file will rearrange your keyboard again if you run it twice, producing garbage. Good thing you have that recovery command in the cut buffer, right?
    You can save the keymap you produce with q2d with “xmodmap -pke > dvorak.xmod” and return to it with “xmodmap dvorak.xmod” after that. If you’re not quite happy with it, you can tweak the xmod file yourself, or get the “xkeycaps” program which will do it for you.


MGL Chapter 2

Filed under: politics — jlm @ 16:50

Chapter 2 of the Massachusetts General Laws makes for endless entertainment.
Start at Section 1 and keep hitting “Next Section” — see how long you can go without cracking up. It starts to get amusing at Section 7 and just gets better from there, and keeps on going, and going, and going! If you make it all the way to section 53, you must be from Vulcan or Massachusetts.


screen tutorial

Filed under: linux — jlm @ 19:37

screen is really simple to use, but it can be hard to get started. It has so many features that the essentials get lost if you try (say) to read the manual. So here they are:

1: Start a new session with screen

This is the first thing to do, but it’s confusing, because when you do it you just get a shell prompt again. What happened? You’re actually in a new shell, inside of screen. To demonstrate, start a long-running command, like cat. Leave it up, and go on to

2: Detach from inside screen with “Ctrl-A d”

You should get a message saying screen detached and a shell prompt. This is the prompt from your old shell: It actually ran the command screen, the stuff between then and now was all inside screen, and now the screen command just exited and we’re back at the old shell. Now you can do whatever, log off, log back in, just don’t reboot, then

3: Reattach screen with screen -r

Hey, hey, we’re back where we were when we ran screen in step 1, and our cat is still there waiting for input! That’s the most fundamental concept, the core of screen: It keeps these pseudo-terminals going for you, while you log off, go to bed, switch computers, whatever. There’s a parent “SCREEN” process that screen made, and as long as that process sticks around, you can go back to your pseudo-terminals with screen -r. There’s one other fundamental:

4: Detach from outside screen with screen -d

When you try to reattach a session which screen already has attached elsewhere, it gives you an error. (Try it from another shell.) Maybe the network kills your connection while you’re in screen. Maybe you absent mindedly left it attached from visiting the Timbuktu regional office. Maybe you had to leave in a hurry and couldn’t do petty housekeeping like detaching your session. But you need to reattach that session here. So screen -d will detach the old connection, and now you can screen -r freely. Try it!

That’s the ultra-basics. When you’re comfortable with them, you can read about how to do copy and paste, multiple windows, multi-display, and lots of other useful features… At leisure. You’ve got the core now.


Combinatorics: Counting with replacement

Filed under: math — jlm @ 05:14

Everyone knows the number of ways you can choose k items out of n without replacement is nCk = n!/k!(n-k)!. Often forgotten is that the number of ways to choose k items from n with replacement is n+k-1Ck. I think this is because a) it doesn’t come up as much, and b) there’s a clear intuitition behind the first formula but not so the second. So, why is the value n+k-1Ck? Proving it with induction is a simple matter, but doesn’t shed much light.

Let’s look at it differently… We can number our items 1, 2, …, n and order the k chosen items accordingly, making a non-decreasing sequence. So the problem becomes counting the non-decreasing sequences a1a2 ≤ … ≤ ak. Turn each sequence into a bar graph, so that for n=7, k=4 the sequence [3,5,5,6] becomes

[bar graph]

We can make the graph into a path from (0,1) to (k,n)

[outline path]

This path has n-1+k steps, n-1 vertical and k horizontal. Choosing one such path is choosing the k horizontal steps from the n+k-1 total, hence n+k-1Ck.

Another way to think about it is to consider the items chosen to be bins to fill with balls: Choosing at item becomes putting a ball in the bin, and the number of balls in a bin represents how many times that item was chosen. So the problem becomes count the number of ways you can put k identical balls in n bins. Start with bin 1. At each step you can put a ball into the current bin, or move to the next bin. You’ll place balls in k of the steps, and move bins in n-1 of them, so it’s a matter of choosing the k steps out of the k+n-1 total in which to place balls.

Powered by WordPress