So, I need to make a function described as
invFib :: Integer -> Maybe Integer
which takes an integer and looks for it in a fibonacci sequence (as described below)
fibs :: [Integer]
fibs = 0:1:(zipWith (+) fibs (tail fibs))
and returns the index of the example number:
invFib 0 ~> Just 0
invFib 1~> Just 1orJust 2
map invFib [54, 55, 56] ~> [Nothing,Just 10,Nothing]
invFib (fibs !! 99) ~> Just 99
I tried to make a function that takes a list of integers and splashes out the index, but it continues to fail. Any thoughts?
this is the 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 hangs on numbers that are not in the fibonacci sequence, it also shows only 1 value when entering 1
invFib :: Integer -> Maybe Integer
invFib n = if n < 0
then Nothing
else fmap fromIntegral (elemIndex n fibs)
source
share