[Haskell-cafe] Code walking off the right edge of the screen

Jochem Berndsen jochem at functor.nl
Sat Jun 20 16:25:29 EDT 2009


Antoine Latter wrote:
> On Sat, Jun 20, 2009 at 1:05 PM, Deniz Dogan<deniz.a.m.dogan at gmail.com> wrote:
>> I (too) often find myself writing code such as this:
>>
>> if something
>>  then putStrLn "howdy there!"
>>  else if somethingElse
>>          then putStrLn "howdy ho!"
>>          else ...
>>
>> I recall reading some tutorial about how you can use the Maybe monad
>> if your code starts looking like this, but as you can see, that
>> doesn't really apply here. "something" and "somethingElse" are simply
>> booleans and each of them have different actions to take if either of
>> them is True.
>>
>> So how do I make code like this prettier?
> 
> I'm not entirely sure if this is haskell'98, but GHC seems to support
> this sort of layout:
> 
> main = do
>   <some computation>
> 
>   if something then someOtherComputation else do
> 
>   <continue at the same indentation>
> <<<<<

IMHO, this is ugly and counterintuitive; I like having a single "point
of exit" (to use an imperative programming term) of a function. Your
suggestion is equivalent to

someComputation;
if something then begin
	someOtherComputation;
	exit;
end;
more;

in, say, Pascal. This obscures the fact that "more;" is sometimes/often
not executed. (You could argue the same about exceptions, but they are a
necessary evil ;-).

Regards,

-- 
Jochem Berndsen | jochem at functor.nl
GPG: 0xE6FABFAB


More information about the Haskell-Cafe mailing list