%lxNSMOCqVM%

February 5th, 2009

%%6LCXPOnb%%

Data Constructors and Readability

February 5th, 2009

I think I’ve put my finger on one reason I’m finding Haskell hard to read. Consider this algebraic data type definition from Real World Haskell:

data Doc = Empty
         | Char Char
         | Text String
         | Line
         | Concat Doc Doc
         | Union Doc Doc
         deriving (Show, Eq)

There’s no distinction between the value constructor and the components of the type. This is especially critical when the constructor and the components have the same name as in the Char constructor above. It also doesn’t help that they have the same naming convention (mixed camel case, initial uppercase letter).

A Java class with multiple factory methods or multiple constructors would be much more verbose, but much more readable. Humans need redundancy, even if it’s logically superfluous.

I suspect a language that retained Haskell’s semantics, but completely replaced the syntax with something more usable, legible, and familiar would have a much greater chance of success.

Not a Yellow Wagtail

January 29th, 2009

side view of bird with streaked breast
American Pipit, San Joaquin Wildlife Sanctuary, 2009-01-29
Read the rest of this entry »

unlines with fold

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?

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

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

Currently Reading