The Author Online Book Forums are Moving

The Author Online Book Forums will soon redirect to Manning's liveBook and liveVideo. All book forum content will migrate to liveBook's discussion forum and all video forum content will migrate to liveVideo. Log in to liveBook or liveVideo with your Manning credentials to join the discussion!

Thank you for your engagement in the AoF over the years! We look forward to offering you a more enhanced forum experience.

376034 (11) [Avatar] Offline
#1
Care to share the solution? Or give more hints
326925 (10) [Avatar] Offline
#2
I think this is Chapter 8 in the new book.
The solution does not compile.
fastfib _ _ 0 = 0
fastFib _ _ 1 = 1
fastFib _ _ 2 = 1
fastFib x y 3 = x + y
fastFib x y c = fastFib(x+y) * (c -1)
326925 (10) [Avatar] Offline
#3
This is the correct solution.
fastFib _ _ 0 = 0
fastFib _ _ 1 = 1
fastFib _ _ 2 = 1
fastFib x y 3 = x + y
fastFib x y c = fastFib(x+y) x (c -1)
fib n = fastFib 1 1 n
Octarine (1) [Avatar] Offline
#4
I think a solution can be one line shorter:
fastFib _ _ 0 = 0
fastFib _ _ 1 = 1
fastFib x y 2 = x + y
fastFib x y c = fastFib y (x+y) (c-1)
fib n = fastFib 0 1 n
396000 (2) [Avatar] Offline
#5
It can be even shorter:
fastFib n1 n2 0 = n2 - n1
fastFib n1 n2 counter = fastFib n2 (n1 + n2) (counter - 1)
fib n = fastFib 1 1 n