This particular problem can be worked around as follows:
myFunc2 (Something x) = returnSomething x
myFunc2 (Another n ys) =
let xs = drop n ys
x = head ys
x' = head (tail ys)
intermediate = some $ treatment xs
in case n of
0 -> doSomething intermediate n
1 -> doSomething1 intermediate n x
2 -> doSomething2 intermediate n x x'
Thanks to lazy evaluation x
and x'
will be only evaluated if their value is needed.
However - and this is a big however! - your code will give a runtime error when you try to call myFunc2 (Another 2 [])
(and if doSomething2
actually uses x
!) because to find out what x
is, we need to evaluate head ys
- and that'll crash for an empty list. The code you gave as an example also won't work (another runtime error) for Another 2 []
since there's no matching pattern, but there it's easier to supply a fall-back case.
This might not be a problem if you control the input and always make sure that the list in Another
is long enough, but it's important to be aware of this issue!