Reputation: 1081
I begin a "new" Fibonacci algorithm, but it's not working. It's very simple:
--fiblista 0 n = [0]
--fiblista 1 n = [1]
fiblista a n
| a <= n = (0:1:tail, ((fiblista!!d)+(fiblista!!c))) fiblista a+1 n
where d = a - 1
c = a - 2
Example:
First error code: parse error on input `='
Any idea how to fix this algorithm? I read thousand of example Fibonacci algorithm in haskell, but I need to write a new one.
Upvotes: 1
Views: 681
Reputation: 47052
Your indentation is wrong. Try:
fiblista 0 n = [0]
fiblista 1 n = [1]
fiblista a n
| a < n = (tail, ((fiblista!!d)+(fiblista!!c))) fiblista a+1 n
where d = a - 1
c = a - 2
But this code still won't compile. fiblista
is a function with two parameters, but you also try to use it as a list. You also try to use a tuple as a function.
If you are still stuck writing your function, it would help if you could describe what the parameters to fiblista
are, and what it should return. Include a type signature.
Is this homework?
So.
fiblista a n
is a list of Fibonacci numbers, starting with the ath Fibonacci number, and ending with the (n-1)th Fibonacci number.
This is a list question.
Imagine you had a list of all the Fibonacci numbers. (This would be the same as fiblista 0 ∞
, if ∞
was a valid Haskell value.) You would then be able to use the standard list functions take
and drop
to calculate fiblista a n
.
Upvotes: 4