Fibonacci calculator in Haskell
up vote
3
down vote
favorite
I'm just starting to look into Haskell. I've written a naive Fibonacci implementation, and I've also written a more advanced one that uses tail-call recursion for efficiency.
module Fibonacci where
import System.Environment
fibonacci :: Integer -> Integer
fibonacci 0 = 0
fibonacci 1 = 1
fibonacci n
| n < 0 = error "Cannot find a negative fibonacci number"
| otherwise = fibonacci (n - 1) + fibonacci (n - 2)
fibonacci' :: Integer -> Integer
fibonacci' n
| n < 0 = error "Cannot find a negative fibonacci number"
| otherwise = fibHelper n 0 1
where
fibHelper :: Integer -> Integer -> Integer -> Integer
fibHelper n a b
| n == 0 = a
| otherwise = fibHelper (n - 1) b (a + b)
firstNumberFrom :: [String] -> Integer
firstNumberFrom = 10
firstNumberFrom args = read $ args !! 0
main = do
args <- getArgs
let num = firstNumberFrom args in
putStrLn $ show (fibonacci' num)
I'd appreciate any reviews on correctness and idiomatic usage.
haskell fibonacci-sequence
bumped to the homepage by Community♦ 24 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
add a comment |
up vote
3
down vote
favorite
I'm just starting to look into Haskell. I've written a naive Fibonacci implementation, and I've also written a more advanced one that uses tail-call recursion for efficiency.
module Fibonacci where
import System.Environment
fibonacci :: Integer -> Integer
fibonacci 0 = 0
fibonacci 1 = 1
fibonacci n
| n < 0 = error "Cannot find a negative fibonacci number"
| otherwise = fibonacci (n - 1) + fibonacci (n - 2)
fibonacci' :: Integer -> Integer
fibonacci' n
| n < 0 = error "Cannot find a negative fibonacci number"
| otherwise = fibHelper n 0 1
where
fibHelper :: Integer -> Integer -> Integer -> Integer
fibHelper n a b
| n == 0 = a
| otherwise = fibHelper (n - 1) b (a + b)
firstNumberFrom :: [String] -> Integer
firstNumberFrom = 10
firstNumberFrom args = read $ args !! 0
main = do
args <- getArgs
let num = firstNumberFrom args in
putStrLn $ show (fibonacci' num)
I'd appreciate any reviews on correctness and idiomatic usage.
haskell fibonacci-sequence
bumped to the homepage by Community♦ 24 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
1
What is your purpose behind implementing a naive fibonacci function? Are you familiar with its the limitations? Are you familiar with more efficient fibonacci algorithms?
– Code-Guru
May 5 at 18:47
1
The Haskell wiki has an article with many different Fibonacci implementations: wiki.haskell.org/The_Fibonacci_sequence
– Code-Guru
May 5 at 18:50
add a comment |
up vote
3
down vote
favorite
up vote
3
down vote
favorite
I'm just starting to look into Haskell. I've written a naive Fibonacci implementation, and I've also written a more advanced one that uses tail-call recursion for efficiency.
module Fibonacci where
import System.Environment
fibonacci :: Integer -> Integer
fibonacci 0 = 0
fibonacci 1 = 1
fibonacci n
| n < 0 = error "Cannot find a negative fibonacci number"
| otherwise = fibonacci (n - 1) + fibonacci (n - 2)
fibonacci' :: Integer -> Integer
fibonacci' n
| n < 0 = error "Cannot find a negative fibonacci number"
| otherwise = fibHelper n 0 1
where
fibHelper :: Integer -> Integer -> Integer -> Integer
fibHelper n a b
| n == 0 = a
| otherwise = fibHelper (n - 1) b (a + b)
firstNumberFrom :: [String] -> Integer
firstNumberFrom = 10
firstNumberFrom args = read $ args !! 0
main = do
args <- getArgs
let num = firstNumberFrom args in
putStrLn $ show (fibonacci' num)
I'd appreciate any reviews on correctness and idiomatic usage.
haskell fibonacci-sequence
I'm just starting to look into Haskell. I've written a naive Fibonacci implementation, and I've also written a more advanced one that uses tail-call recursion for efficiency.
module Fibonacci where
import System.Environment
fibonacci :: Integer -> Integer
fibonacci 0 = 0
fibonacci 1 = 1
fibonacci n
| n < 0 = error "Cannot find a negative fibonacci number"
| otherwise = fibonacci (n - 1) + fibonacci (n - 2)
fibonacci' :: Integer -> Integer
fibonacci' n
| n < 0 = error "Cannot find a negative fibonacci number"
| otherwise = fibHelper n 0 1
where
fibHelper :: Integer -> Integer -> Integer -> Integer
fibHelper n a b
| n == 0 = a
| otherwise = fibHelper (n - 1) b (a + b)
firstNumberFrom :: [String] -> Integer
firstNumberFrom = 10
firstNumberFrom args = read $ args !! 0
main = do
args <- getArgs
let num = firstNumberFrom args in
putStrLn $ show (fibonacci' num)
I'd appreciate any reviews on correctness and idiomatic usage.
haskell fibonacci-sequence
haskell fibonacci-sequence
edited May 6 at 3:19
200_success
127k15149412
127k15149412
asked May 5 at 18:29
cbojar
2,3452818
2,3452818
bumped to the homepage by Community♦ 24 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
bumped to the homepage by Community♦ 24 mins ago
This question has answers that may be good or bad; the system has marked it active so that they can be reviewed.
1
What is your purpose behind implementing a naive fibonacci function? Are you familiar with its the limitations? Are you familiar with more efficient fibonacci algorithms?
– Code-Guru
May 5 at 18:47
1
The Haskell wiki has an article with many different Fibonacci implementations: wiki.haskell.org/The_Fibonacci_sequence
– Code-Guru
May 5 at 18:50
add a comment |
1
What is your purpose behind implementing a naive fibonacci function? Are you familiar with its the limitations? Are you familiar with more efficient fibonacci algorithms?
– Code-Guru
May 5 at 18:47
1
The Haskell wiki has an article with many different Fibonacci implementations: wiki.haskell.org/The_Fibonacci_sequence
– Code-Guru
May 5 at 18:50
1
1
What is your purpose behind implementing a naive fibonacci function? Are you familiar with its the limitations? Are you familiar with more efficient fibonacci algorithms?
– Code-Guru
May 5 at 18:47
What is your purpose behind implementing a naive fibonacci function? Are you familiar with its the limitations? Are you familiar with more efficient fibonacci algorithms?
– Code-Guru
May 5 at 18:47
1
1
The Haskell wiki has an article with many different Fibonacci implementations: wiki.haskell.org/The_Fibonacci_sequence
– Code-Guru
May 5 at 18:50
The Haskell wiki has an article with many different Fibonacci implementations: wiki.haskell.org/The_Fibonacci_sequence
– Code-Guru
May 5 at 18:50
add a comment |
1 Answer
1
active
oldest
votes
up vote
1
down vote
The many approaches in main and firstNumberFrom can be unified:
main = print . fibonacci' . maybe 10 read . listToMaybe =<< getArgs
The explicit recursion in fibbonacci' is captured by iterate:
fibbonacci' n = fst $ iterate ((a,b) -> (b, a+b)) (0,1) !! n
add a comment |
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
up vote
1
down vote
The many approaches in main and firstNumberFrom can be unified:
main = print . fibonacci' . maybe 10 read . listToMaybe =<< getArgs
The explicit recursion in fibbonacci' is captured by iterate:
fibbonacci' n = fst $ iterate ((a,b) -> (b, a+b)) (0,1) !! n
add a comment |
up vote
1
down vote
The many approaches in main and firstNumberFrom can be unified:
main = print . fibonacci' . maybe 10 read . listToMaybe =<< getArgs
The explicit recursion in fibbonacci' is captured by iterate:
fibbonacci' n = fst $ iterate ((a,b) -> (b, a+b)) (0,1) !! n
add a comment |
up vote
1
down vote
up vote
1
down vote
The many approaches in main and firstNumberFrom can be unified:
main = print . fibonacci' . maybe 10 read . listToMaybe =<< getArgs
The explicit recursion in fibbonacci' is captured by iterate:
fibbonacci' n = fst $ iterate ((a,b) -> (b, a+b)) (0,1) !! n
The many approaches in main and firstNumberFrom can be unified:
main = print . fibonacci' . maybe 10 read . listToMaybe =<< getArgs
The explicit recursion in fibbonacci' is captured by iterate:
fibbonacci' n = fst $ iterate ((a,b) -> (b, a+b)) (0,1) !! n
edited May 8 at 20:54
answered May 8 at 19:47
Gurkenglas
2,738511
2,738511
add a comment |
add a comment |
Thanks for contributing an answer to Code Review Stack Exchange!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
Use MathJax to format equations. MathJax reference.
To learn more, see our tips on writing great answers.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fcodereview.stackexchange.com%2fquestions%2f193746%2ffibonacci-calculator-in-haskell%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
1
What is your purpose behind implementing a naive fibonacci function? Are you familiar with its the limitations? Are you familiar with more efficient fibonacci algorithms?
– Code-Guru
May 5 at 18:47
1
The Haskell wiki has an article with many different Fibonacci implementations: wiki.haskell.org/The_Fibonacci_sequence
– Code-Guru
May 5 at 18:50