Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Converting between C# List and F# List

Tags:

c#

f#

Remark: This is a self-documentation, but if you have any other suggestions, or if I made any mistakes/miss out something obvious, I would really appreciate your help.

Sources:

convert .NET generic List to F# list

What can I do to pass a list from C# to F#?

https://msdn.microsoft.com/en-us/library/4kf43ys3(v=vs.110).aspx?cs-save-lang=1&cs-lang=fsharp#code-snippet-1


I am a beginner in F# and C#, and I want to convert a C# List to an F# List, and vice versa. The code/commands for doing so changes depending on where is your code (in C# or F#).

Please see my answer below. If you have any other suggestions, please let me know. Thank you.

like image 461
CH Ben Avatar asked Apr 07 '17 06:04

CH Ben


People also ask

What's the conversion between Celsius and Fahrenheit?

C° to F°: Celsius to Fahrenheit Conversion Formula To convert temperatures in degrees Celsius to Fahrenheit, multiply by 1.8 (or 9/5) and add 32.

How do you convert F to C easily?

The temperature conversion is easy to do: Take the °F temperature and subtract 32. Multiply this number by 5. Divide this number by 9 to obtain your answer in °C.

What is the formula of converting Celsius into Fahrenheit and Fahrenheit into Celsius?

Fahrenheit to Celsius is the conversion of a temperature value from the Fahrenheit scale to the Celsius scale. The relationship between Fahrenheit and Celsius is expressed with the formula, °C = (°F - 32) × 5/9; where C represents the value in Celsius and F represents the value in Fahrenheit.


1 Answers

Inside a C# document, I can use ListModule.OfSeq and .ToList(), like this:

// Currently inside a C# document
var CS_list = new List<int> {1,2,3}; 
var FS_list = ListModule.OfSeq(CS_List); // Microsoft.FSharp.Collections.ListModule
var CS_converted_back = FS_List.ToList();

Inside an F# document, I can use Seq.toList and ResizeArray<_>, like this:

// Currently inside an F# document
let FS_list = [1;2;3]
let CS_list = ResizeArray<int> FS_list // System.Collections.Generic.List
let FS_converted_back = Seq.toList CS_list

If I made any mistakes, or if I miss out something obvious, please let me know.

like image 56
CH Ben Avatar answered Sep 30 '22 22:09

CH Ben