I have this function (creates a fibonacci sequence):
unfoldr (\(p1, p2) -> Just (p1+p2, (p1+p2, p1)) ) (0, 1)
Here I notice a repeating expression p1+p2that I would like to take into account, so that it is evaluated only once. The supplement itself is not 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, it is f p1 p2calculated twice (unless there is some optimization of the magic compiler that I do not know about), which could create a performance bottleneck if fit took a lot of calculations. I can not turn f p1 p2in where, because p1, and p2not included in the scope. What is the best way to express this expression so that it is fevaluated only once?
guhou source
share