So i need to make a function described as
invFib :: Integer -> Maybe Integer
which takes an Integer and looks for it in the fibonacci sequence (as described by the function below)
fibs :: [Integer]
fibs = 0:1:(zipWith (+) fibs (tail fibs))
and returns the index of the number example:
invFib 0
~> Just 0
invFib 1
~> Just 1
or Just 2
map invFib [54, 55, 56]
~> [Nothing,Just 10,Nothing]
invFib (fibs !! 99)
~> Just 99
I tried making a function that takes a list of integers and spits out the index, but it keeps failing. Any thoughts?
this is function i tried-
findNum :: [Integer] -> Integer -> Integer -> Integer
findNum x:xs y z = if x == y
then z
else findNum xs y (z+1)
Edit: the function freezes on numbers not in the fibonacci sequence, also only shows 1 value when 1 is entered
invFib :: Integer -> Maybe Integer
invFib n = if n < 0
then Nothing
else fmap fromIntegral (elemIndex n fibs)