I have this function (produces the fibonacci sequence):
unfoldr (\(p1, p2) -> Just (p1+p2, (p1+p2, p1)) ) (0, 1)
In here, I notice a repeated expression, p1+p2
, which I would like to factor so that it is only calculated once. Addition itself isn't an expensive calculation, but for a more general version:
unfoldr (\(p1, p2) -> Just (f p1 p2, (f p1 p2, p1)) ) (0, 1)
where f = arbitrary, possibly time-consuming function
In the above situation, f p1 p2
is calculated twice (unless there's some magic compiler optimisation I don't know about), which could create a performance bottleneck if f
required a lot of computation. I can't factor f p1 p2
into a where
because p1
and p2
are not in scope. What is the best way to factor this expression so that f
is only calculated once?
unfoldr (\(p1, p2) -> let x = f p1 p2 in Just (x, (x, p1)) ) (0, 1)
where f = arbitrary, possibly time-consuming function
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