[Haskell-cafe] Coding conventions for Haskell?

aditya siram aditya.siram at gmail.com
Mon Sep 27 17:09:28 EDT 2010


How do you guys indent long function arguments? I run into this all
the time with the 'maybe' function which takes 3 arguments:
maybe :: b -> (a -> b) -> Maybe a -> b
I usually end up doing things like (pretend the arguments are aligned
if you're not using a monospace font to view this):
maybe do-if-Nothing
          (\x -> do-if-Just x)
          maybe-value
This gets a little unwieldly if the any of the arguments stretch over
one line like:

maybe do-if-Nothing
          (\x -> ...
                  ...
                  something
          )
          maybe-value


Any advice on indentation? I could avoid the problem by adding a 'let'
or 'where' but sometimes I like to show the entire function without
the user having to scan another definition.
-deech




On Mon, Sep 27, 2010 at 3:57 PM, Andrew Coppin
<andrewcoppin at btinternet.com> wrote:
>  On 27/09/2010 02:44 PM, Daniel Fischer wrote:
>>
>> On Monday 27 September 2010 14:52:18, Henning Thielemann wrote:
>>>
>>> data Foo a b =
>>>      Foo    a
>>>    | Bar      b
>>>    | Foobar a b
>>>
>>> avoids this, at least for the type name "Foo".
>>
>> Tastes vary, but I find that ugly. I much rather have the '=' aligned with
>> the '|'.
>>
>> data Foo a b
>>     = Foo      a
>>     | Bar        b
>>     | Foobar  a b
>>       deriving (Eq, Ord)
>>
>> There, that looks good.
>
> Tastes do indeed vary. To me, both of these are incorrect, and the correct
> way is
>
>  data Foo a b =
>      Foo    a   |
>      Bar      b |
>      Foobar a b
>    deriving (Eq, Ord)
>
> It honestly annoys me that Haddock disagrees with me on this point...
>
> (It also irritates me that almost all Haskell identifiers are camel-case,
> but with an inital lowercase letter. IMHO, the correct thing to do is use
> camel-case for identifiers that must begin with an uppercase letter, and
> underscores for identifiers that must begin with a lowercase letter. Of
> course, my opinion has no effect on the Prelude and so forth.)
>
> I generally try to structure my code so that all blocks indent by 2 spaces,
> and the size of indentation never depends on the length of an identifier. In
> other words, none of this:
>
>  foo x y z = do
>              thing1 x
>              thing2 x y
>              thing3 z
>              ...
>
> Do that a few times and you rapidly end up with lines 300 characters wide.
> (!) Instead, I prefer
>
>  foo x y z = do
>    thing1 x
>    thing2 x y
>    thing3 z
>    ...
>
> But, as they say, everybody has their own ideas about style. I think the
> most important point must surely be that any style is applied
> *consistently*...
>
> _______________________________________________
> Haskell-Cafe mailing list
> Haskell-Cafe at haskell.org
> http://www.haskell.org/mailman/listinfo/haskell-cafe
>


More information about the Haskell-Cafe mailing list