Google Groups no longer supports new Usenet posts or subscriptions. Historical content remains viewable.
Dismiss

Use of 'where' - Haskell

3 views
Skip to first unread message

Ben

unread,
Nov 18, 2003, 3:16:33 PM11/18/03
to
I have a question regarding programming style in haskell. When should you
use where and when should you separate functions??

I would say that where should be used where some operation is not going to
be required by any other function, or by itself?? Thus encapsulating the
functionality of a method completely within a method. However this gets
messy...


Code examples (ignoring the fact that these functions are pretty stupid!):

use where example:-
doSomething :: Integral a => a -> a
doSomething 0 = 0
doSomething x = doSomethingDifferent(doSomethingElse x)
where
doSomethingElse y = y * constant * 10
where constant z = 543

doSomethingDifferent z
| z < 6000 = 1
| otherwise = 2


use seperate functions example:
doSomething :: Integral a => a -> a
doSomething 0 = 0
doSomething x = doSomethingDifferent(doSomethingElse x)

doSomethingElse :: Integral a => a -> a
doSomethingElse y = y * constant * 10
where constant z = 543

doSomethingDifferent :: Integral a => a -> a
doSomethingDifferent z
| z < 6000 = 1
| otherwise = 2

Thanks for your help,

Ben


Darius

unread,
Nov 18, 2003, 3:46:02 PM11/18/03
to
On Tue, 18 Nov 2003 20:16:33 -0000
"Ben" <n...@email.com> wrote:

> I have a question regarding programming style in haskell. When should
> you use where and when should you separate functions??

Depends. Some cases are quite clear, e.g. a local accumulator function
or avoiding excessive parameter passing (see AvoidingParameterPassing on
the Haskell wiki). In other cases, it's a trade-off between potential
reusability and having to pass in some parameters and filling up the
(module) namespace.

Luckily, HaRe, the (young) Haskell Refactorer, supports this refactoring
so changing your mind is a few keystrokes.

> I would say that where should be used where some operation is not
> going to be required by any other function, or by itself?? Thus
> encapsulating the functionality of a method completely within a
> method. However this gets messy...
>
> Code examples (ignoring the fact that these functions are pretty
> stupid!):

You may also want to look at http://www.haskell.org/hawiki/HaskellStyle
for tips on how to visually arrange your code.

Tomasz Zielonka

unread,
Nov 18, 2003, 4:06:29 PM11/18/03
to
Ben wrote:
> I have a question regarding programming style in haskell. When should you
> use where and when should you separate functions??
>
> I would say that where should be used where some operation is not going to
> be required by any other function, or by itself?? Thus encapsulating the
> functionality of a method completely within a method. However this gets
> messy...

Notice that functions and values defined in where clause can refer to
parameters of enclosing function clause. This can be very convenient.

For example:

data Record =
Record
{ recSum :: Integer
, recMin :: Integer
, recMax :: Integer
, recProduct :: Integer
}
deriving Show

combineRecords r1 r2 =
Record
{ recSum = combine recSum (+)
, recMin = combine recMin min
, recMax = combine recMax max
, recProduct = combine recProduct (*)
}
where
combine selector op = selector r1 `op` selector r2
-- ^^ ^^

In this example the use of where combined with lexical scoping allows me
to avoid some redundance and it's easier to see that the code is
correct. Compare this to:

combineRecords' r1 r2 =
Record
{ recSum = recSum r1 + recSum r2
, recMin = recMin r1 `min` recMin r2
, recMax = recMax r1 `max` recMax r2
, recProduct = recProduct r1 * recProduct r2
}

In this case there are more things I have to check.

Best regards,
Tom

--
.signature: Too many levels of symbolic links

Ben

unread,
Nov 18, 2003, 6:20:16 PM11/18/03
to
Thanks for the help Darius and Tomasz


0 new messages