asInt_either

Tuesday, January 20th, 2009

Real World Haskell, Exercise 4, p. 98:

The asInt_fold function uses error, so its callers cannot handle errors. Rewrite it to fix this problem.
(more…)

transpose

Sunday, January 18th, 2009

Real World Haskell, Exercise 4, p. 84:

Write a program that transposes the text in a file. For instance, it should convert “hello\nworld\n” to “hw\neo\nlr\nll\nod\n”.
(more…)

Haskell: Concatenation vs. Prepending

Wednesday, January 14th, 2009

This confused me a little until I grokked it. In Haskell, an item is not the same as a list containing the one item. (Obvious, I know, but some languages like XQuery take the opposite view).

The : operator prepends an item to the beginning of a list. It can only be used to insert an item at the beginning of a list. It cannot be used to append an item to the end of a list. Thus x:xs is correct but xs:x is a syntax error. (Haskell’s design seems stuck with assumptions going all the way back to Lisp and 1950s era processors including that the singly linked list is a good implementation for the list abstract data type. This has other consequences. For instance, length is an O(n) operation. And now that I look for it, insert into the middle of the list also seems to be missing.)

Lists are concatenated with the ++ operator, not :.

So, to sum up:

  • Use : to insert an item at the beginning of a list.
  • Use ++ to join two lists together.
  • To append an item to a list, insert the item into an empty list, and then concatenate the two lists together.
  • More generally, to insert an item into a list at any position other than the first:
    1. Split the list into two sublists at the insertion point with splitAt.
    2. Use : to prepend the new item to the second list.
    3. Concatenate the two lists back together with ++

Am I missing something obvious here?

How Long Does It Take to Compile Haskell?

Monday, December 22nd, 2008

I’ve been compiling ghc from MacPorts on this 2.0 GHz MacBook, and I’m beginning to wonder if it’s hung. I’ve been stuck on “Building ghc” for quite a while, maybe an hour:

--->  Cleaning perl5.8
--->  Fetching ghc
--->  Attempting to fetch ghc-6.10.1-src.tar.bz2 from http://haskell.org/ghc/dist/stable/dist/
--->  Attempting to fetch ghc-6.10.1-src-extralibs.tar.bz2 from http://haskell.org/ghc/dist/stable/dist/
--->  Attempting to fetch ghc-6.8.2-darwin-i386-leopard-bootstrap.tar.bz2 from http://haskell.org/ghc/dist/6.8.2/
--->  Verifying checksum(s) for ghc
--->  Extracting ghc
--->  Applying patches to ghc
--->  Configuring ghc
--->  Building ghc

Certainly it was long enough to answer a bunch of e-mails, and edit an article. Is this the point where it actually compiles the compiler? Is ghc self-hosting? That is, is ghc written in ghc? That may be what the bootstrap bit is about. Yep, looks like it is.

I do remember 2 hour gcc compiles, but that was 15 years ago on much slower hardware. How long does it take to compile a compiler nowadays?