[ create a new paste ] login | about

Link: http://codepad.org/vT5tgfqQ    [ raw code | output | fork | 1 comment ]

mohit_at_codepad - Haskell, pasted on Mar 16:
1
2
3
4
5
6
7
8
9
10
11
12
13
-- Written by Mohit Jain
-- Calculate nth fibonacci number
main = printFibonacci 20

printFibonacci :: Int -> IO ()
printFibonacci = print . calculateFibonacciAtIndex

calculateFibonacciAtIndex n = fibList !! pred n where
                              fibList = map getFib [1..]
                              getFib 1 = 1
                              getFib 2 = 1
                              getFib n = getFib (n-1) + getFib (n-2)
                              -- getFib n = calculateFibonacciAtIndex (n-1) + calculateFibonacciAtIndex (n-2)


Output:
1
6765


Create a new paste based on this one


Comments:
posted by mohit_at_codepad on Mar 20
Review comment: This piece of code is not readily testable. Write main function in such a way that it looks for user input. (Command line / console or file redirection)
In command line case, if input is not supplied, a dummy / default input can be used. (like 20 in this case)
reply