Skip to content

Instantly share code, notes, and snippets.

@qoelet
Created August 1, 2013 04:20
Show Gist options
  • Save qoelet/6128384 to your computer and use it in GitHub Desktop.
Save qoelet/6128384 to your computer and use it in GitHub Desktop.
Fibonacci task
-- file: Fib.hs
-- Task: Generate first n Fibonacci numbers, then sum it
-- fib: given n for the index of the Fibonacci series, return the corresponding value, e.g. fib 9 gives 34
fib :: Int -> Int
fib 0 = 0
fib 1 = 1
fib n = fib (n-1) + fib (n-2)
-- fib_n: given n for 0..n as the indexes of the Fibonacci series, return the values
fib_n :: Int -> [Int]
fib_n 0 = []
fib_n n = fib_n (n - 1) ++ [fib n]
-- task completion: sum (fib_n n)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment