Generating Fibonacci numbers in Haskell?

前端 未结 11 1011
有刺的猬
有刺的猬 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:06

    using iterate

    fibonaci = map fst (iterate f (0,1)) where f (x,y) = (y,x+y)
    

    using

    take 10 fibonaci
    
    [0,1,1,2,3,5,8,13,21,34,55,89,144,233,377]
    

提交回复
热议问题