Generating Fibonacci series in F# -


I'm just starting to learn F # using VS2010 and my first attempt at making Fibonacci series down is what I do I'm trying to make a list of all the 400 numbers.

  Let's Fabbelist = Let's L = [1; 2;] Let's change the unstable one = 1 selective B = 2 while L. Tail and lieutenant; Let 400 give C = A + BL.Ad (C) Let A = B be B = C  

My first problem is that on the final statement, I get an error message On the last line "Incomplete structural construction at or before this point in expression" I do not know why I am doing wrong here.

Although this is a clear way to prepare a list (from C ++ / C # programmers) in a very efficient manner, however, I do not feel like I know about F Does the right way to program do I am right in this spirit?

First of all, if you are using as There was a statement to change a variable, but this is not the case. F # is used to declare a new value in let (which can hide any previous value of the same name). If you want to write code using mutation, then you have to do something like this:

  let c = a + b // declare the new local value. (C) a & lt; B // mutate value to be 'ineligible' b & lt; - c //. The second issue with your code  

The second issue with your code is that you want to change the F # list element to add it - F # lists are immutable, so when you make them , You can not modify them (in particular, no add is not a member!). If you want to use this mutation, you can write:

  let's create fablist = // a temporary list, so that we can add elements // (this is the standard .NET 'List & lt; t & gt;' Type ') L = Resizing New RTA & lt; _ & Gt; ([1; 2]) Let's instability = 1 selective B = 2 while L. L [number1] & lt; 400 Let be C = A + BL.Add (C) // Add element to the mutual list & lt; - b b & lt; - c l | & Gt; List.ofSeq // Convert any collection type to the standard F # list  

But, as others have already mentioned, writing code like this is not idle F # solution is. In F #, you will use recurring (like while ) instead of unchanging lists and loops. For example:

  // Recursive function which implements looping // (it takes the last two elements, A and B) give REC fibers RCE B = if A + B & LT ; 400 then // current element, let's present = a + b // calculate all the remaining elements gradually - use 'B' (in the next journey) as 'A' and 'current' in the form of 'B' Please. // Return the remaining elements in the front of the list as a result (this creates a new list, // there is no change here!) Current :: The rest of the rest [] // Generated all the elements - Returns empty After the list, make a list with 1, 2, and other large Fibonacci fibers = 1 :: 2 :: (Fibercrack 2)  

Comments