I know that the f# list is not the same at the c# List. What do I need to do to be able to pass a list of ints from a c# application to an f# library? I'd like to be able to use pattern matching on the data once it's in the f# code.
You can send any data types of argument to a function (string, number, list, dictionary etc.), and it will be treated as the same data type inside the function.
C always uses 'pass by value' to pass arguments to functions (another term is 'call by value', which means the same thing), which means the code within a function cannot alter the arguments used to call the function, even if the values are changed inside the function.
You can use
Seq.toList : IEnumerable<'a> -> list<'a>
to convert any IEnumerable<'a>
seq
to an F# list. Note that F# lists are immutable; if you want to work with the mutable list, you don't need to do anything special, but you won't be able to use pattern matching. Or, rather, you can define active patterns for System.Collections.Generic.List<'a>
; it's just a bad idea.
Here is how I ended up doing it.
The FSharp code:
let rec FindMaxInList list =
match list with
| [x] -> x
| h::t -> max h (FindMaxInList t)
| [] -> failwith "empty list"
let rec FindMax ( array : ResizeArray<int>) =
let list = List.ofSeq(array)
FindMaxInList list
The c Sharp code:
List<int> myInts = new List<int> { 5, 6, 7 };
int max = FSModule.FindMax(myInts);
You can pass a sequence of ints - it's basically anything that supports IEnumerable<int>
.
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With