### Golden Search Algorithm in F#

#### Juan C LR on May 18, 2018

Start
I wrote the code for the Golden Search algorithm in python for one of my university classes, I really found this method interest...
[Read Full]

Start
I wrote the code for the Golden Search algorithm in python for one of my university classes, I really found this method interest...
[Read Full]

Like someone already mentioned your code is imperative, not functional.

The first clue is that functional code does not use mutability.

Your implementation is not using recursion just a loop, that is the second clue. To use recursion in F# you need to add

`rec`

before the function name, like below where it says:`let rec golden ...`

A functional version could look like this:

This code demonstrates several traits of F#:

`|>`

) and even the triple pipe! (`|||>`

) to avoid unnecessary names`limits`

becomes`(a, b)`

when calling:`golden 1 limits`

Inside

`goldenAlgorithm`

is the recursive function`golden`

of type:`int -> float * float -> (int * float * float)`

It receives the number of iteration (starting with 1) and the limits a & b.

It calls itself using tail recursion until it finds a solution and returns a triplet with the total number of iterations, the solution x and the value of f(x).

My suggestion to you is to not abandon functional programming. You are just starting and the more you learn the more you are going to like it, I promise.

Your reason for sticking with OOP is mainly because that is your comfort zone right now, real growth happens when you venture out of your comfort zone.

I really like how you approached the Golden Algorithm and how you implemented it.

Very interesting your article! Nice job implementing the Golden Algorithm!👌🏼

You are evaluating the function too often. You should only need to evaluate once for each iteration. You can see this if you put a printfn in the function and see the repeated same calls.

This is not functional programming, you are implementing a loop and modifying variables, so it's imperative. If it were functional, you would instead use a recursive function :)

Excellent article! Great job Juan C LR!