Not a Yellow Wagtail

Thursday, January 29th, 2009

side view of bird with streaked breast
American Pipit, San Joaquin Wildlife Sanctuary, 2009-01-29
(more…)

unlines with fold

Tuesday, January 27th, 2009

Real World Haskell, Exercise 10, p. 99: This was an easy one:

myUnlines :: [String] -> String
myUnlines xs = foldr (addLine) "" xs 
    where addLine c acc = acc ++ c ++ "\n"

It’s pretty much straight down the path of exactly what folds are meant for, though the inferred types still managed to surprise me, and my initial implementation was about twice this size.

Did We Actually Elect a Good President?

Monday, January 26th, 2009

It’s still early days, but Wow:

A democracy requires accountability, and accountability requires transparency. As Justice Louis Brandeis wrote, “sunlight is said to be the best of disinfectants.” In our democracy, the Freedom of Information Act (FOIA), which encourages accountability through transparency, is the most prominent expression of a profound national commitment to ensuring an open Government. At the heart of that commitment is the idea that accountability is in the interest of the Government and the citizenry alike.

The Freedom of Information Act should be administered with a clear presumption: In the face of doubt, openness prevails. The Government should not keep information confidential merely because public officials might be embarrassed by disclosure, because errors and failures might be revealed, or because of speculative or abstract fears. Nondisclosure should never be based on an effort to protect the personal interests of Government officials at the expense of those they are supposed to serve. In responding to requests under the FOIA, executive branch agencies (agencies) should act promptly and in a spirit of cooperation, recognizing that such agencies are servants of the public.

All agencies should adopt a presumption in favor of disclosure, in order to renew their commitment to the principles embodied in FOIA, and to usher in a new era of open Government. The presumption of disclosure should be applied to all decisions involving FOIA.

The presumption of disclosure also means that agencies should take affirmative steps to make information public. They should not wait for specific requests from the public. All agencies should use modern technology to inform citizens about what is known and done by their Government. Disclosure should be timely.

–President Barack Obama

Read the rest in Freedom of Information Act

It’s starting to seem like Barack Obama actually gets it. I still want to see him prosecute the criminals in the Bush administration, but otherwise it’s starting to look like we’ve elected a good one for the first time in my lifetime. Fingers crossed.

words with fold

Monday, January 26th, 2009

Real World Haskell, Exercise 10, p. 98: Write the words function using list folds:

myWords :: String -> [String]
myWords s = reverse (foldr step [] (trim s))

step :: Char -> [String] -> [String]
step c [] = [c:[]]
step c acc 
    | (isSpace c) = acc ++ [""]
    | otherwise = (take ((length acc) - 1) acc) ++ [c:(last acc)]

isSpace :: Char -> Bool
isSpace ' '  = True
isSpace '\n' = True
isSpace '\r' = True
isSpace '\t' = True
isSpace _    = False

lstrip :: String -> String
lstrip [] = ""
lstrip (x:xs) 
  | (isSpace x) = lstrip xs
  | otherwise = x:xs

rstrip :: String -> String
rstrip s = reverse (lstrip (reverse s))

trim :: String -> String
trim = lstrip . rstrip

Moral of this exercise: you can only pass a list to ++. You can’t use ++ to append a Char to a String. You have to append [c] instead.
(more…)

My Subcategories Vanished

Monday, January 26th, 2009

OK. This is weird. WordPress has lost all my subcategories. E.g. I can assign a post to Software Development but not Software Development/Haskell. OK, I seem to have gotten them back by clicking “Add New Category.” Probably some strange AJAX bug.

Hypothesis: cycles cannot be implemented only with folds

Saturday, January 24th, 2009

The cycle function turns a list into an infinite list by repeating it. For instance, cycle [1,2,3] is [1,2,3,1,2,3,1,2,3...]. I could be wrong but I don’t think you can do this one purely with folds and here’s why:
(more…)