[Haskell-cafe] let vs. where

hjgtuyl at chello.nl hjgtuyl at chello.nl
Thu Nov 15 11:53:48 EST 2007


On Thu, 15 Nov 2007 12:31:07 +0100, Henning Thielemann  
<lemming at henning-thielemann.de> wrote:
> On Tue, 13 Nov 2007, Dan Piponi wrote:
>> On Nov 13, 2007 1:24 PM, Ryan Ingram <ryani.spam at gmail.com> wrote:
>> > I tend to prefer where, but I think that guards & function  
>> declarations are
>> > more readable than giant if-thens and case constructs.
>>
>> Up until yesterday I had presumed that guards only applied to
>> functions. But I was poking about in the Random module and discovered
>> that you can write things like
>>
>> a | x > 1 = 1
>>   | x < -1 = -1
>>   | otherwise = x
>
> Btw. I would write here
>   min 1 (max (-1) x)
>  or even better define a function for such clipping, since it is needed
> quite often.

The value of 'a' needs only be calculated once; when defined at top level,  
'a' is a CAF; in a 'where' clause, the value is also calculated once.

-- 
Met vriendelijke groet,
Henk-Jan van Tuyl


--
http://functor.bamikanarie.com
http://Van.Tuyl.eu/
--



More information about the Haskell-Cafe mailing list