3
let a = [1;2;3;]

for i in (a |> Seq.take 10) do Console.WriteLine(i)
for i in (a |> Seq.take 100) do Console.WriteLine(i)

first line works well but second line gives error : The input sequence has an insufficient number of elements.

Yes , there is no 100 elements , they are just 3 but why 10 works then ?

Online test

after all it works on C#

using System;
using System.Linq;

class P
{ static void Main() {

 var p = new[] {1,2,3,4};

 foreach(var i in p.Take(10).ToArray()) Console.WriteLine(i);
 foreach(var i in p.Take(2).ToArray()) Console.WriteLine(i);
 foreach(var i in p.Take(100).ToArray()) Console.WriteLine(i);
}}

Online test

cnd
  • 32,616
  • 62
  • 183
  • 313
  • 2
    Seq.take 10 doesn't work for me either. you'd better double check it. – Nyi Nyi Apr 04 '11 at 12:51
  • Compare your online-test with this: http://ideone.com/WAyj0 It fails during the first loop already. It just prints the first 3 elements, before failing. – Joachim Sauer Apr 04 '11 at 13:07
  • @Joachim Sauer ... yes I already understood. – cnd Apr 04 '11 at 13:18
  • 2
    Downvotes a little unfriendly here I think, OP made a simple mistake, but took time to document the question well and is clearly trying out F# and has pointed out a difference between take in F# vs C# – Chris Ballard Apr 04 '11 at 20:43

3 Answers3

8

It's printing out 3 elements and then printing out the error message.

Jeff Foster
  • 43,770
  • 11
  • 86
  • 103
6

Other answers have explained your mistake (and I recommend trying not to jump to conclusions about compiler bugs, you'll probably be downvoted). Also, you're comparing Seq.take with Enumerable.Take, but they don't have the same behavior. However, Seq.truncate does have the same behavior as Enumerable.Take

Mauricio Scheffer
  • 98,863
  • 23
  • 192
  • 275
  • I see . Output confused me, I think let x = Enumerable.Take myList 5 must works then. – cnd Apr 04 '11 at 13:27
  • +1 for linking to a blog post that mentions that `Seq.truncate` does what the OP is expecting `Seq.take` to do, but it wouldn't hurt to say so explicitly. – Joel Mueller Apr 04 '11 at 17:16
4

in your sample second for loop is not executed at all. first one outputs 1 2 3 and then throws exception

desco
  • 16,642
  • 1
  • 45
  • 56