F# Concat two tuples without deconstructing - f#

I have two tuples of unknown length which I would like to add together resulting in a third tuple.
For example:
// given two tuples
let tuple1 = (1, 2)
let tuple2 = (3, 4, 5)
// resulting in
let tuple3 = (1, 2, 3, 4, 5)
A solution I came up with was to deconstruct both tuples first and then create a new tuple like this:
let a b = tuple1
let c d e = tuple2
let tuple4 = (a, b, c, d, e)
However this will not work with a tuple of unkown length.
Another solution solution could be:
let tuple5 = tuple1, tuple2
But this results in a tuple of two tuples like this:
(int * int) * (int * int * int)
Is there a tuple function I'm missing? Is there anything else I'm overlooking? Thanks.

The design of F# tuples means that you need to write code that knows the length of a tuple - tuples of different lengths have different types and you cannot write code that's generic over the length of a tuple.
If you want a data structure that allows you to store arbitrary number of values and concatenate them, it is probably better to use lists, which allow concatentation using the # operator:
let list1 = [1; 2]
let list2 = [3; 4; 5]
let list3 = list1 # list2
The caveat is that lists can only store values of one type - such as int in your example. If you need to store arbitrary number of arbitrary things, you will probably need a list of discriminated unions.

The explanation is that tuples are not meant to be used as lists (which may have unknown length). Think of tuples instead as very simple structs or classes where the members are implicitly named "first", "second", "third", etc. So your question is like answering "how do I concat the members of two structs"? The answer could be reflection but most likely hints at some problem when modelling the data types for your program.
Typical use cases for tuples are very simple ad-hoc data types or other cases where the length and the order of the members are natural. For example:
type Point2D = float * float
type Point3D = float * float * float
type Size = int * int
type Error = int * string

This is not an answer, since it only demonstrates the futility of attempting to work around F# type system for tuples. But technically, it is indeed possible to combine tuples of various lengths with a generic function, which could consume its arguments in a type-safe way; alas, it will possess an unspecified return type that requires annotation. This is done by employing two features that, rightly so, do not figure prominently in functional programming:
Method overload resolution, in combination with statically resolved type parameters, for the domain
Reflection for the range
There are a couple of other limitations: We are not able to encode the 1-tuple, such an overload cannot uniquely be resolved since it may stand for an n-tuple in turn. A similar reason precludes the use of two tupled tuples of varying lengths as direct argument for the static members.
type X = X with
static member ($) (_ : X, (a, b)) = [|box a; box b|]
static member ($) (_ : X, (a, b, c)) = [|box a; box b; box c|]
let inline t2a x = X $ x
open Microsoft.FSharp.Reflection
let a2t arg : 'r =
FSharpValue.MakeTuple(arg, typeof<'r>) :?> 'r
let inline (##) a b =
Array.append (t2a a) (t2a b) |> a2t
let (t : int * char * int64 * string) = (1, 'b') ## (3L, "d")
// val t : int * char * int64 * string = (1, 'b', 3L, "d")
let (u : int * int * int * int * int) = (1, 2, 3) ## (4, 5)
// val u : int * int * int * int * int = (1, 2, 3, 4, 5)
Given the above, the concatenation of a 4-tuple with a 2-tuple will produce a compile-time error.
let (v : int * int * int * int * int * int) = (1, 2, 3, 4) ## (5, 6)
// error FS0001: No overloads match for method 'op_Dollar'.

Related

F# filtering a list (newbie)

I'm recently new to f# so please bear with me. the problem i have is I'm trying to display only yo parts of a list but unsure of the coding for example
int * int * float * float * int * float * float =
but only want
int * int
any help would be appreciated
So one way would be
l |> List.map (fun (a,b,_,_,_,_,_) -> (a,b))
The example you presented:
int * int * float * float * int * float * float
is a signature of a tuple. It means your tuple has 7 items of float and int type in the order specified in the signature. So an example of it would be:
let myTuple = (2, 4, 6.0, 8.0, 10, 12.0, 14.0)
You can create a function to extract first two items and ignore remaining ones.
let frstAndScd (a, b, _, _, _, _, _) = (a, b)
The underscore means that you are not interested in these items. You only take those marked by letters and return them as a new tuple.

Finding a subset, but F# will not let me compare two sets of different lengths

I'm trying to find if one set is a subset of the other. My code works perfectly if both sets are the same length.
For example,
My code will return true if
x = [a;b;c]
y = [a;b;c]
My code will return false if
x = [a;b;c]
y = [a;b;d]
However, my code will not even compile if I try
x = ['a';'b';'c']
y = ['a';'b';'c';'d']
It's supposed to return true, but I get the following error message:
Type mismatch.
Expecting a Set<char * char * char> but given a Set<char * char * char * char>
The tuples have differing lengths of 3 and 4
My code is below
let mySubList x y =
printfn "%A is a proper subset of %A: %b" x y (Set.isSubset x y)
let x = Set.empty.Add('a','b','c')
let y = Set.empty.Add('a','b','c', 'd')
let z = Set.empty.Add('a','x','a','y','c','e')
let found = mySubList x y
All your sets contain the same number of elements: they all contain exactly one element. However they contain elements of different types, which is why you can't compare. Namely x contains a 3-tuple (char * char * char), y contains a 4-tuple and z contains a 5 tuple.
The important thing to note here is that Add('a', 'b', 'c') doesn't call Add with multiple arguments - it calls it with one argument, which is a tuple. This will add a single tuple element to the set. To add multiple elements, use Add multiple times or just use the set on a list (i.e. set ['a'; 'b'; 'c']) instead of Add to avoid multiple function calls.

Converting a list of strings into floats/ints in F#

Is there a quick and simple way to convert an entire list of strings into floats or integers
and add them together similar to this in F#?
foreach(string s in list)
{
sum += int.Parse(s);
}
If you want to aim for minimal number of characters, then you can simplify the solution posted by Ganesh to something like this:
let sum = list |> Seq.sumBy int
This does pretty much the same thing - the int function is a generic conversion that converts anything to an integer (and it works on strings too). The sumBy function is a combination of map and sum that first projects all elements to a numeric value and then sums the results.
Something like this should have the same effect:
let sum = list |> Seq.map System.Int32.Parse |> Seq.sum
F# doesn't seem to support referring to the method on int so I had to use System.Int32 instead.
In F# the type seq is an alias for the .NET IEnumerable, so this code works on arrays, lists etc.
Note the use of Parse in "point-free" style - a function without its argument can be used directly as an argument to another function that expects that type. In this case Seq.map has this type:
('a -> 'b) -> seq<'a> -> seq<'b>
And since System.Int32.Parse has type string -> int, Seq.map System.Int32.Parse has type seq<string> -> seq<int>.
Technically, there are at least 3 different approaches:
1) The Seq.sum or sumBy approach described in the other answers is the canonical way of getting the sum in F#:
let sum = Seq.sumBy int list
2) For instructional purposes, it may be interesting to see how closely one can simulate C# behavior in F#; for instance, using a reference cell type:
let inline (+=) x y = x := !x + y
let sum = ref 0
for s in list do sum += int s
3) Same idea as 2), but using a byref pointer type:
let inline (+=) (x:_ byref) y = x <- x + y
let mutable sum = 0
for s in list do &sum += int s

Does F# treat parameter matching differently when there is only one input parameter?

The function matching is based on the definition of the file in F#:
let f2 x y = x + y
let value5 = f2 10 20
let value = f2(10, 20) <-- Error
let f3 (x, y) = x + y
let value6 = f3(10, 20)
let value = f3 10 20 <-- Error
However, I can use in both ways with one parameter with F#:
let f n = n + 10
let value3 = f 10
let value4 = f(10)
Why is this? Does F# treat parameter matching differently when there is only one input parameter?
As ashays correctly explains, the two ways of declaring functions are different. You can see that by looking at the type signature. Here is an F# interactive session:
> let f1 (x, y) = x + y;;
val f1 : int * int -> int
> let f2 x y = x + y;;
val f2 : int -> int -> int
The first function takes a tuple of type int * int and returns int. When calling it, you need to specify the tuple (which is just a single value):
// Using tuple directly as the argument
f1 (1, 2)
// .. or by declaring tuple value first
let tup = (1, 2)
f1 tup
The type of the second function is int -> int -> int, which is the same thing as int -> (int -> int). This means that it is a function that takes int and returns a function that takes int and returns int. This form is called curried form and it allows you to use partial function application as demonstrated by ashays. In fact, the call:
f2 1 2
// Could be written as:
(f2 1) 2
My suspection is that this has something to do with tuples and currying. Basically, a tuple of one item becomes a singular item again, however in our other two cases we have the following:
The first case (f2) is actually a function that takes a single value (x) and returns a value that takes another single function. Here we can see the use of currying from f2 to add10
let add10 = f2 10
let myVal = add10 20
We get an error with the tuple because we have not defined it in such a way as to receive a tuple. In the second example, we have a similar issue, where we defined the function to take a tuple of two values, and it knows how to process those values, but we have passed it two values now instead of the one (a tuple) that it was expecting, and thus we receive an error.
Once again, in the last case, we have a tuple of a single item and so f x and f(x) are effectively the same thing.
I could be wrong in my reasoning, but I believe that's why you're getting your errors.

Does F# iterate through tuples from back to front?

I've created a little tuple of langauges and when using it in the interactive window they are listed in reverse. Is this normal F# bahavior?
let languages = ("English", "Spanish", "Italian")
let x, y, z = languages
val languages : string * string * string = ("English", "Spanish", "Italian")
val z : string = "Italian"
val y : string = "Spanish"
val x : string = "English"
You're creating three variables, at the same time, with independant values. Order is not relevant here. F# interactive could print the values in any order.
What is important is order evaluation in your code, and the spec says it's from left to right when you're calling a function or constructor, creating a record, and so on.
> (printfn "a", printfn "b");;
a
b
That is also how FSI prints tuples when I decompose them them on my machine.
eg:
let x, y = ("a", "b");;
val y : string = "b"
val x : string = "a"
It's a little weird that it prints in "reverse", but I'm not sure that I would call it F# behavior as much as it is FSI behavior or pretty print behavior.
If you're want all the details, you can always have a look at the source code:
http://fsharppowerpack.codeplex.com/
I'm not sure if there is a connection, but wrapping F# expressions in Quotations can give you insight into the semantics of the language, and you can see in the following that the named values are indeed bound in reverse order like shown in FSI:
> <# let languages = ("English", "Spanish", "Italian") in let x, y, z = languages in () #> |> string;;
val it : string =
"Let (languages,
NewTuple (Value ("English"), Value ("Spanish"), Value ("Italian")),
Let (z, TupleGet (languages, 2),
Let (y, TupleGet (languages, 1),
Let (x, TupleGet (languages, 0), Value (<null>)))))"
Note that this is still consistent with #Laurent's answer, which asserts that argument expressions in tuple construction are evaluated from left to right. In the following example, see how the result of the tuple construction is bound to an intermediate named value, which is then deconstructed using side-effects free TupleGet expressions.
> <# let x,y = (stdin.Read(), stdin.ReadLine()) in () #> |> string;;
val it : string =
"Let (patternInput,
NewTuple (Call (Some (Call (None, System.IO.TextReader ConsoleIn[Object](),
[])), Int32 Read(), []),
Call (Some (Call (None, System.IO.TextReader ConsoleIn[Object](),
[])), System.String ReadLine(), [])),
Let (y, TupleGet (patternInput, 1),
Let (x, TupleGet (patternInput, 0), Value (<null>))))"

Resources