transpose

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”.
Read the rest of this entry »

Turning Off Captions

January 16th, 2009

Let’s see if this worked. If you don’t see a caption it worked:

A Jumping spider on top of my monitor
Read the rest of this entry »

2008 Birding Retrospective

January 15th, 2009

2008 was a year I moved to a new state on the other side of the country, and a part of that state I’d only been to once before. It also featured trips to China and Louisiana. The net result was one of the biggest years I’ve had in a long time. According to eBird, my year total was somewhere over 300 species. I don’t know exactly because I don’t enter the China data there. Even more impressively, 2008 saw 61 life birds, 12 from China, 3 from Louisiana, and 46 from California. That’s not even counting some new exotics like this Red-whiskered Bulbul from Huntington Library and Gardens:

Crested bird, perched
Read the rest of this entry »

A Constitutional Thought Experiment for California

January 15th, 2009

Suppose the voters of this state passed a ballot resolution banning marriage between doctors and lawyers, and further invalidating existing marriages between doctors and lawyers. Would such a resolution be binding, or would it rightly be rejected by the courts? Are there any limits on the power of a ballot resolution beyond those set by the Federal Constitution?

Planning for BGBY 2009

January 14th, 2009

I finished 2008 with 156 BGBY species, well behind the 200+ Neil Gilbert racked up in the same county, and he’s not as well situated as I am. In my defense I didn’t start until February, and was also dealing with a move to a new state. I didn’t even get my bike repaired from damage the movers did to it until April, and I lost a couple of weeks in the middle of Spring migration on a trip to China. With some effort, this year I might be able to crack 200.

However doing that is going to take some work. In particular:
Read the rest of this entry »

Haskell: Concatenation vs. Prepending

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?

Currently Reading