Given:
open System.Linq
this is an acceptable expression:
[2; 3; 4].SelectMany(fun n -> { 1..n })
However, it is not:
[2; 3; 4].SelectMany(fun n -> [ 1..n ])
The error message says:
int -> int list
is not compatible with type
int -> System.Collections.Generic.IEnumerable<'a>
F # rejects the expression because the function returns int list.
Consider this C # program that does something like this:
using System;
using System.Collections.Generic;
using System.Linq;
namespace SelectManyCs
{
class Program
{
static List<int> Iota(int n)
{
var ls = new List<int>();
for (var i = 0; i < n; i++) ls.Add(i);
return ls;
}
static void Main(string[] args)
{
var seq = new List<int>() { 2, 3, 4 };
foreach (var elt in seq.SelectMany(Iota))
Console.WriteLine(elt);
}
}
}
Iotareturns a List<int>and passing it to is SelectManyacceptable for C #.
Is F #'s design behavior or bug? If this is by design, why does a similar operation work in C #?
source
share