Generating Fibonacci numbers in Haskell?

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

    going by the definition, every item of the fibonacci series is the sum of the previous two terms. putting this definition in to lazy haskell gives u this!

    fibo a b = a:fibo b (a+b)
    

    now just take n items from fibo starting with 0,1

    take 10 (fibo 0 1)
    

提交回复
热议问题