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.










share|improve this question
















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















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.










share|improve this question
















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













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.










share|improve this question















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






share|improve this question















share|improve this question













share|improve this question




share|improve this question








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














  • 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










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





share|improve this answer























    Your Answer





    StackExchange.ifUsing("editor", function () {
    return StackExchange.using("mathjaxEditing", function () {
    StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
    StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
    });
    });
    }, "mathjax-editing");

    StackExchange.ifUsing("editor", function () {
    StackExchange.using("externalEditor", function () {
    StackExchange.using("snippets", function () {
    StackExchange.snippets.init();
    });
    });
    }, "code-snippets");

    StackExchange.ready(function() {
    var channelOptions = {
    tags: "".split(" "),
    id: "196"
    };
    initTagRenderer("".split(" "), "".split(" "), channelOptions);

    StackExchange.using("externalEditor", function() {
    // Have to fire editor after snippets, if snippets enabled
    if (StackExchange.settings.snippets.snippetsEnabled) {
    StackExchange.using("snippets", function() {
    createEditor();
    });
    }
    else {
    createEditor();
    }
    });

    function createEditor() {
    StackExchange.prepareEditor({
    heartbeatType: 'answer',
    convertImagesToLinks: false,
    noModals: true,
    showLowRepImageUploadWarning: true,
    reputationToPostImages: null,
    bindNavPrevention: true,
    postfix: "",
    imageUploader: {
    brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
    contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
    allowUrls: true
    },
    onDemand: true,
    discardSelector: ".discard-answer"
    ,immediatelyShowMarkdownHelp:true
    });


    }
    });














    draft saved

    draft discarded


















    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

























    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





    share|improve this answer



























      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





      share|improve this answer

























        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





        share|improve this answer














        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






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited May 8 at 20:54

























        answered May 8 at 19:47









        Gurkenglas

        2,738511




        2,738511






























            draft saved

            draft discarded




















































            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.




            draft saved


            draft discarded














            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





















































            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







            Popular posts from this blog

            Feedback on college project

            Futebolista

            Albești (Vaslui)