April 16th, 2004

  • evan

more haskell and language ramblings

More playing with Haskell.

Collapse )

Collapse )

I think I first became curious about the language from reading about graydon (long before he was on LJ, even!) writing nice things about it. So thanks!

Also: I managed to make the interpreter segfault. I tried to reproduce it with a smaller test case, but I couldn't. It only occurred when I shadowed a variable:
let c = ...
let c = ...

If I renamed the second one to c' it would run happily. (And only renamed it: it wasn't like I used the renamed one differently.)

* As always, there are exceptions that get stranger and stranger, like polysynthetic languages or Prolog / J.
  • evan

ugh

I got all confused with that last post. What I was trying to say was:
  1. Human languages pretty much all do the noun-verb thing, and variation in structure is relatively minimal. Programming languages have a pretty small problem domain, but approach it a wide variety of ways.
  2. Human languages have a large lexicon, and programming languages usually don't. The neat thing about Haskell's lexicon is that there's so much abstraction that stuff connects together in all sorts of neat ways, so mastery of the lexicon allows truly succinct expressions of solutions much in the way a large vocabulary allows for shorter sentences. I spotted cool tricks like these while I was reading through the prelude:
    reverse          =  foldl (flip (:)) []
    (That colon is the cons operator, and "flip" takes a function of two arguments and produces the same function with its arguments in the other order.)
    any p            =  or . map p
    all p            =  and . map p
    (all predicate list evaluates to True if the predicate function is true for the whole list. The period is the function composition operator.)</i>

Speaking of language hacks: functional languages often have a problem with reading from files, because the "read" function typically either reads into a buffer, or returns a different string on each call. Haskell's answer?
readFile   :: FilePath -> IO String
It returns a list of chars that is the entire file. A waste of memory? No, because evaluation is lazy: bytes are only read from the file as other parts of the program evaluate those parts of the list.
Unfortunately, it's probably still inefficient because it reads a byte at a time from the file. But it's still neat.