wiki:Functions/Fib

Fibonacci numbers

fibs = 0 : 1 : [ a+b | (a,b) <- zip fibs (tail fibs) ]

or

{-# LANGUAGE ParallelListComp #-}

fibs = 0 : 1 : [ a+b | a <- fibs | b <- tail fibs ]

almost perfect (assuming perfection is applicable to an imperative style)

func fib(n int) int {
    x, y := 0, 1
    for i := 0; i < n; i++ {
        x, y = y, x+y
    }
    return x
}

absolutely beautiful decorator (declarative instead of imperative) from python stdlib

from functools import lru_cache

@lru_cache(maxsize=None)
def fib(x):
   if x is 0 or x is 1:
        return 1
    else:
        return fib(x-1) + fib(x-2)
Last modified 3 years ago Last modified on Nov 6, 2017, 11:59:28 AM
Note: See TracWiki for help on using the wiki.