[Haskell-cafe] Why does Haskell have the if-then-else syntax?

Brian Hulley brianh at metamilk.com
Thu Jul 27 13:49:42 EDT 2006


David House wrote:
> On 27/07/06, Brian Hulley <brianh at metamilk.com> wrote:
>> I think there's really only one way when it needs to occupy more
>> than one line:
>>
>>        if c
>>              then t
>>              else f
>
> Confusingly,
>
> if c
> then t
> else f
>
> Also works, although no-one really knows why.

Only if the "if" does not start a new layout line. Anyway, how about 
changing the syntax to:

    "if" exp "{" "then" exp ";" "else" exp "}"

Then the layout rule + the offside rule would still allow (iiuc)

    if x < 0 then 5 else 6

but would force the "then" to be further indented than the "if" (and the 
"else" to be indented at least as much as the "then" (indenting it more is 
where the offside rule is needed to make things work))

In addition, if we followed Jon's suggestion to make constructs 
self-bracketing, we could allow an optional keyword such as "/if" to 
terminate the construct early thus:

    a = if x < 0 then 5 else 6 /if + 78

I'd also change the lambda syntax to:

    \{x 2 -> x+5; x y -> x*y}

which again would, by the layout rule, still allow current lambda syntax as 
a special case. The optional terminator could be "/\" and if all constructs 
were now aexp's (as suggested by the desire to make them self-bracketing) 
instead of exp10's we could then write:

     f \x y -> y x /\ 6

instead of having to write

     f (\x y -> y x) 6

I'd be in favour of /if /case /let /\ etc instead of fi esac tel because it 
looks more systematic and follows the usual XML conventions for end tags. 
I'd suggest that floating point division should just be written `divide` - 
it's just a very specialised arithmetic op so why waste a nice symbol on it? 
(ditto ^ ^^ **) (I'd have thought integer division is used more often and 
no-one seems to mind writing `div`.)

Anyway having said all this, I can't help feeling that explicit brackets, as 
required at the moment, help to clarify the structure of the code, and that 
removing the need for them may negatively impact on readability.

Regards, Brian.

-- 
Logic empowers us and Love gives us purpose.
Yet still phantoms restless for eras long past,
congealed in the present in unthought forms,
strive mightily unseen to destroy us.

http://www.metamilk.com 



More information about the Haskell-Cafe mailing list