Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can i convert between F# List and F# Tuple?

Tags:

Is there some way to convert between F# List and F# Tuple?

For example:

[1;2;3] -> (1,2,3)     (1,2,3,4) -> [1;2;3;4] 

I need two Functions to do that:

let listToTuple list = ... let tupleToList tuple = ... 

Thank you in advance.

like image 781
kev Avatar asked May 27 '10 10:05

kev


People also ask

How do you convert F to C mentally?

If you want to convert Fahrenheit to Celsius, do the opposite: subtract 30 from the temperature in degrees Fahrenheit, and then divide by 2 to get the temperature in degrees Celsius. = 27 °C (In reality, 84 °F is equivalent to 28.89 °C—again, that's a pretty close estimate!).


1 Answers

Besides listToTuple then pblasucci has the right answer. But you wont be happy with the result unless you know something about type types involved, or if you wan't to do a lot of boxing and unboxing.

let tupleToList t =      if Microsoft.FSharp.Reflection.FSharpType.IsTuple(t.GetType())          then Some (Microsoft.FSharp.Reflection.FSharpValue.GetTupleFields t |> Array.toList)         else None  let listToTuple l =     let l' = List.toArray l     let types = l' |> Array.map (fun o -> o.GetType())     let tupleType = Microsoft.FSharp.Reflection.FSharpType.MakeTupleType types     Microsoft.FSharp.Reflection.FSharpValue.MakeTuple (l' , tupleType) 
like image 139
Huusom Avatar answered Oct 20 '22 01:10

Huusom