Generating Fibonacci numbers in Haskell?

前端 未结 11 1063
有刺的猬
有刺的猬 2020-11-29 18:35

In Haskell, how can I generate Fibonacci numbers based on the property that the nth Fibonacci number is equal to the (n-2)th Fibonacci number plus the (n-1)th Fibonacci numb

11条回答
  •  孤街浪徒
    2020-11-29 19:19

    A lazy way of generating infinite Fibonacci series can easily be achieved by unfoldr as follows;

    fibs :: [Integer]
    fibs = unfoldr (\(f,s) -> Just (f,(s,f+s))) (0,1)
    

提交回复
热议问题