Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 7835017
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 2, 20262026-06-02T13:26:40+00:00 2026-06-02T13:26:40+00:00

here is 2 functions that calculate the fibbonaci number, they both work correctly, but

  • 0

here is 2 functions that calculate the fibbonaci number, they both work correctly, but are written in a different way.

which one would you consider better, more efficient, code more understandable ?

let fibe n =
    let rec loop acc1 acc2 n = 
        match n with
        | n when n = 0I -> acc1        
        | x -> loop acc2 (acc1 + acc2) (x - 1I)
    loop 0I 1I n

let myfib n =
    if n = 0I then 0I
    else if n = 1I then 1I
    else
    let rec loop i f s = 
        match i with 
        | x when x = n -> f+s 
        | x when x < n -> loop (i+1I) s (s+f)        
    loop 2I 0I 1I
  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-02T13:26:43+00:00Added an answer on June 2, 2026 at 1:26 pm

    Talking of clarity, both your functions IMO are unnecessary cluttered and use inadequate language mechanisms for the purpose.

    Generating Fibonacci is an ideal fit for expressing via unfold, like below:

    let fibnum n =
        let fibnums = Seq.unfold (fun (current, next) -> 
                    Some(current, (next, current+next)))(0I,1I)
        fibnums |> Seq.nth n
    

    How may you make it further shorter and clearer?

    UPDATE: as the question’s author considers the ability to operate with Fibonacci members having sequence numbers of bigint as important, the above snippet definitely can be adopted to this requirement, although getting less straightforward:

    let fibnum bigN = 
        let fibnumsI =
            ((0I,0I),(1I,0I))
            |> Seq.unfold (fun ((current, idx), (next, idx)) ->  
                Some((current, idx),((next, idx + 1I), (current+next, idx + 1I))))
        fibnumsI |> Seq.skipWhile (fun (x,i) -> i < bigN) |> Seq.head |> fst
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

this code is an Integral program that calculate functions f(x) and here .. the
I have created a function to calculate the difference of 2 timeval functions. Here
I'm dealing with neural networks here, but it's safe to ignore that, as the
Here is the function that i am using: $('.urlz').click(function (event) { event.preventDefault(); var url
I've got a function here that is meant to look through a list of
Here is a simple function that converts a string to an integer. int str2int(char
Here is an (artificial) example of using a function that returns an anonymous struct
Here's a brief rundown of my issue: I have a JavaScript function that gets
Here is what I'm trying to do: - i need a function that when
I have a function that uses the Google Blobstore API, and here's a degenerate

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.