Skip to content
Snippets Groups Projects
Commit 9d3d394a authored by Matt Bovel's avatar Matt Bovel
Browse files

Add fixedPoint curry version

parent 200d6414
No related branches found
No related tags found
No related merge requests found
...@@ -59,10 +59,18 @@ import scala.annotation.tailrec ...@@ -59,10 +59,18 @@ import scala.annotation.tailrec
def fixedPoint(f: Int => Int): Int => Int = def fixedPoint(f: Int => Int): Int => Int =
@tailrec @tailrec
def rec(x: Int): Int = def loop(guess: Int): Int =
val y = f(x) val image: Int = f(guess)
if x == y then x else rec(y) if image == guess then guess else loop(image)
rec loop
```
Or alternatively, using currying:
```scala
def fixedPointCurry(f: Int => Int)(guess: Int): Int =
val image: Int = f(guess)
if image == guess then guess else fixedPoint(f)(image)
``` ```
- `fixedPoint(x => x/2)(4)` returns `0`. - `fixedPoint(x => x/2)(4)` returns `0`.
......
0% Loading or .
You are about to add 0 people to the discussion. Proceed with caution.
Please register or to comment