5
votes

Can 2 or more equations defining a function in Haskell share the same where / let block?

Let me present a contrived example to illustrate the question.

First, consider the following code as a starting point:

someFunction v1 v2 v3 = difference ^ v3
   where
      difference = v1 - v2

So far, so good. But then, imagine I need to deal with an "alternative case", where I need to return zero if v3 == 99 and difference < 4 (completely arbitrary, but let's say those are my requirements).

My first thought would be to do this:

someFunction v1 v2 99 | difference < 4 = 0
someFunction v1 v2 v3 = difference ^ v3
   where
      difference = v1 - v2

However, that won't work because the first equation for someFunction and the second equation for someFunction are not both sharing the same where block. This is not a big deal in this contrived example because there is only one variable in the where block ("difference"). But in a real world situation, there could be a large number of variables, and it would be unacceptable to repeat them.

I already know how to solve this by using guards and having only one equation. The question is, is there a way for multiple equations to share the same where / let clause? Because it seems desirable to have multiple equations with different patterns instead of being forced to have just one equation with many guards.

2

2 Answers

13
votes

One option would be to lift the function into the where block itself:

someFunction v1 v2 = f
    where
        f 99 | difference < 4 = 0
        f v3 = difference ^ v3
        difference = v1 - v2
0
votes

I think you can't. Probably your best solution is something like:

someFunction v1 v2 v3 | v3==99 && difference<4 = 0
                      | otherwise = difference ^ v3
                      where difference = v1 - v2