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 794025
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 14, 20262026-05-14T22:14:56+00:00 2026-05-14T22:14:56+00:00

I’m just starting to learn F# using VS2010 and below is my first attempt

  • 0

I’m just starting to learn F# using VS2010 and below is my first attempt at generating the Fibonacci series. What I’m trying to do is to build a list of all numbers less than 400.

let fabList = 
    let l =  [1;2;]
    let mutable a = 1
    let mutable b = 2
    while l.Tail < 400 do
        let c = a + b
        l.Add(c)
        let a = b
        let b = c

My first problem is that on the last statement, I’m getting an error message “Incomplete structured construct at or before this point in expression” on the last line. I don’t understand what I’m doing wrong here.

While this seems to be an obvious way to build the list in a fairly efficient way (from a c++/C# programmer), from what little I know of f#, this doesn’t seem to feel to be the right way to do the program. Am I correct in this feeling?

  • 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-05-14T22:14:57+00:00Added an answer on May 14, 2026 at 10:14 pm

    First of all, you’re using let as if it was a statement to mutate a variable, but that’s not the case. In F#, let is used to declare a new value (which may hide any previous values of the same name). If you want to write code using mutation, then you need to use something like:

    let c = a + b  // declare new local value
    l.Add(c)  
    a <- b   // mutate value marked as 'mutable'
    b <- c   // .. mutate the second value
    

    The second issue with your code is that you’re trying to mutate F# list by adding elements to it – F# lists are immutable, so once you create them, you cannot modify them (in particular, there is no Add member!). If you wanted to write this using mutation, you could write:

    let fabList = 
      // Create a mutable list, so that we can add elements 
      // (this corresponds to standard .NET 'List<T>' type)
      let l = new ResizeArray<_>([1;2])
      let mutable a = 1
      let mutable b = 2
      while l.[l.Count - 1] < 400 do
        let c = a + b
        l.Add(c) // Add element to the mutable list
        a <- b
        b <- c
      l |> List.ofSeq // Convert any collection type to standard F# list
    

    But, as others already noted, writing the code in this way isn’t the idiomatic F# solution. In F#, you would use immutable lists and recursion instead of loops (such as while). For example like this:

    // Recursive function that implements the looping
    // (it takes previous two elements, a and b)
    let rec fibsRec a b =
      if a + b < 400 then
        // The current element
        let current = a + b
        // Calculate all remaining elements recursively 
        // using 'b' as 'a' and 'current' as 'b' (in the next iteration)
        let rest = fibsRec b current  
        // Return the remaining elements with 'current' appended to the 
        // front of the resulting list (this constructs new list, 
        // so there is no mutation here!)
        current :: rest
      else 
        [] // generated all elements - return empty list once we're done
    
    // generate list with 1, 2 and all other larger fibonaccis
    let fibs = 1::2::(fibsRec 1 2)
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I'm making a simple page using Google Maps API 3. My first. One marker
I have just tried to save a simple *.rtf file with some websites and
link Im having trouble converting the html entites into html characters, (&# 8217;) i
That's pretty much it. I'm using Nokogiri to scrape a web page what has
I am trying to understand how to use SyndicationItem to display feed which is
Basically, what I'm trying to create is a page of div tags, each has
I have a string like this: La Torre Eiffel paragonata all&#8217;Everest What PHP function
I am reading a book about Javascript and jQuery and using one of the
I'm using v2.0 of ClassTextile.php, with the following call: $testimonial_text = $textile->TextileRestricted($_POST['testimonial']); ... and

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.