Today’s Programming Praxis problem is an easy one: all we have to do is make a function that calculates the golden ratio. Sadly, the provided solution already has the easiest way to do this, so all we can do is use the Haskell equivalent.
Since we want real fractions instead of floating point numbers, we’re going to need the Data.Ratio package.
To calculate the golden ratio, we repeatedly take the reciprocal and add one, starting with 1 for the first step.
golden :: Int -> Rational golden n = iterate (succ . recip) 1 !! n
A simple test to show it works correctly:
main :: IO () main = print $ golden 200
And that’s all there is to it. Piece of cake.