Evan Martin (evan) wrote in evan_tech,
Evan Martin

haskell is still blowing my mind

Haskell is still blowing my mind.

In fifteen lines of code I have a simple parser for templated text which reports errors intelligently and knows how to dump its parse tree:
*HTML> parse parseTemplate "" "foo bar"
Right [RawText:"foo bar"]
*HTML> parse parseTemplate "" "foo ^baz^ bar"
Right [RawText:"foo ",Variable:"baz",RawText:" bar"]
*HTML> parse parseTemplate "" "foo ^baz bar"
Left (line 1, column 9):
unexpected " "
expecting letter or "^"

Then in ten more lines of code I have a renderer that can run templates and report errors:
*HTML> let testTemplate = (\(Right x) -> x) (parse parseTemplate "" "demo: x is ^x^ and foo is ^foo^")
-- the bit with the "Right" is just to skip the "did it parse ok" check and wouldn't be in real code
*HTML> renderTemplate testTemplate [("x","meow"),("foo","bar")]
Right "demo: x is meow and foo is bar"
*HTML> renderTemplate testTemplate [("x","meow")]
Left "unknown variable foo"

But what's the most awesome about this all is that the language doesn't have exceptions or special support for parsing or anything; all of this can be accomplished with their unified monad syntax and a lot of abstraction. The static typing also means there ought to be no gotchas like an exception I forgot to handle or a type error.

  • blog moved

    As described elsewhere, I've quit LiveJournal. If you're interested in my continuing posts, you should look at one of these (each contains feed…

  • dremel

    They published a paper on Dremel, my favorite previously-unpublished tool from the Google toolchest. Greg Linden discusses it: "[...] it is capable…

  • treemaps

    I finally wrote up my recent adventures in treemapping, complete with nifty clickable visualizations.

  • Post a new comment


    default userpic
    When you submit the form an invisible reCAPTCHA check will be performed.
    You must follow the Privacy Policy and Google Terms of use.
  • 1 comment