I have a function with multiple patterns. I have two or more of them which share the same expression which I want to replace. Now if I write a where
clause at the bottom, indent it and define a new variable as the expression I wanted to replace it won't work.
Example:
myFunction firstParam secondParam = expression
myFunction firstParam _ = 1 + expression
where expression = firstParam + secondParam
Compiler message:
Not in scope: `expression'
Not in scope: `secondParam'
How do I do it?
You can factor out the pattern matches into a case. For example:
myFunction :: Int -> Int -> Int
myFunction a b = case (a, b) of
(0, 4) -> x
(_, b) -> x + b
where
x = a + b
Here x
is visible in both case branches.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With