13 comments

[ 1.8 ms ] story [ 38.1 ms ] thread
The next language on my "Languages to learn" list is Haskell. I'm a big Clojure & Lisp fan, and Haskell seems like it would be an amazing study in contrast with the Lisp family I'm thrilled by.

However, this makes me cringe a little:

  +++ thediv ! [identifier "gallery"] 
      << (thediv ! [theclass "screenshots"]
      << ulist << 
         (li << anchor ! [href ("view.fcgi?file=" ++ first)]
As far as I know, Haskell doesn't a code-as-data philosophy. (Right?) I think that there are a lot of pure Lisp HTML alternatives, and they work because Lisp is at the same time declarative and functional. S-expressions are concise and almost look like CSS. But Haskell code doesn't seem like such a great replacement. There's too much syntax, and, in my opinion, that gets in the way of understanding-at-a-glance.

Is this style common in Haskell Web development?

  > Is this style common in Haskell Web development?
Not at all; the author is quite obviously inexperienced in using Haskell, and is using some weird old crufty library to create HTML.

The two most common template libraries are Heist[1] and Hamlet[2]. Heist uses Rails/Django-style external template files, while Hamlet embeds markup directly into the Haskell source code using quasiquotation.

[1] http://snapframework.com/docs/tutorials/heist

[2] http://www.yesodweb.com/book/templates

Even if one wanted to represent markup as Haskell types, there's no reason it needs to be as ugly as in that example. A really basic version might be:

  div [("id", "gallery")] $ do
    div [("class", "screenshots")] mzero
    ul [] $ do
      li [] $ do
        a [("href", "view.fcgi?file=" ++ first)] mzero
Also, "getLastSixImages" should be obvious to any experienced functional programmer as something which is begging for a higher-order function.
and selecting the last six images in one query, instead of this six times:

  SELECT name FROM images WHERE number = ((SELECT max(number) FROM images) - 1

  > quite obviously inexperienced in using Haskell
I'm using Haskell for only four months now, I'd be worried if it weren't obvious.

Besides, it's not as bad as it looks.

+++ just concatenates two HTML fragments

<< puts something under a HTML element

the ! [...] defines attributes for a HTML element

Pretty clear, but I guess to each his own.

Anything like HAML for Haskell? I looked at the tutorials you linked to, and Heist and Hamlet looks like very poor substitutes. Hamlet seems inspired by HAML, but they seemed to have missed the point.

Here's the equivalent HAML:

    #gallery
      .screenshots
      ul
        li
          a{"href" => "view.fcgi?file=#{first}"}
HAML is super clean, and there's essentially no redundant syntax; in other words, if you type something, it's because it's necessary. There's no fat.
actually the attribute syntax leaves a lot to be desired in terms of redundancy. First you have to quote the attributes (or put a : in front of them?), yuck. you need to use the => operator which is two times too big, and whats with the unnecessary outer braces?

non-idiomatic hamlet isn't far from that haml snippet (note you would never put bits of urls in the template as strings, instead you provide a url rendering function which converts the @{} bits into url strings):

    <#gallery
      <.screenshots
      <ul
        <li
          <a href=@{View first}
idiomatic hamlet is much more focused on being a cleaner html for designers than it is trying to be a haml clone.
> actually the attribute syntax leaves a lot to be desired in terms of redundancy.

I agree. Considering that you can assign 'id' and 'class' through the main syntax, I don't think it's too bad. Attribute values are Ruby strings, so the quotes work for me. With Ruby 1.9 you should, in theory, be able to use the new improved hash syntax:

    a{href: "/ding"}
...but I don't know if HAML has implemented it.

Edit: HAML actually supports a different, simpler syntax for attributes:

    %a(href="/ding")
> idiomatic hamlet is much more focused on being a cleaner html for designers than it is trying to be a haml clone.

I know that some companies work that way. We let our designers learn HAML (and SASS) instead. That way they can contribute to the projects as first-class citizens. HAML is simple enough that a designer can even work with conditionals and loops.

> There's too much syntax.

Actually there's very little syntax in that snippet. all those scary +++, <<, etc. are just functions and you can look at how they're implemented, ask GHCi what their type is, etc.

But I agree that doesn't make the code above any less readable :)

(comment deleted)
(comment deleted)
(comment deleted)
myfromJust has a too specific type, and really should just be inlined as:

  fromMaybe ""
checkInputFile encodes a no-result as a "no" string. This is a very un-Haskelly thing to do (Use a Maybe String, instead).

Use of C's if syntax is a bit weird, too. No need for these extra parens.

getRandomName weirdly uses getStdGen/newStdGen instead of just making a random generator directly.

basename is (incorrectly) duplicating standard library behavior.

saveFile uses fromJust, will crash at runtime.