Evan Martin (evan) wrote in evan_tech,
Evan Martin
evan
evan_tech

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.
Subscribe

  • livejournal kids

    Neat image from Jack Dorsey. Every so often someone will ask me about Twitter and I'll dig up a a random day from Brad's LJ in 1999 and talk about…

  • megaupload captcha

    Someone make a Javascript-based captcha cracker for megaupload. It's strange to see those captchas again because I idly myself wrote a…

  • zombie ghosd

    I was tickled to discover another IBM developerworks article on one of my abandoned hacks and that both it and its predecessor have been translated…

  • Post a new comment

    Error

    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