2014-11-21 45 views
0

我寫了這個C#代碼來獲取一組單詞的每個子序列。F#中的每個子序列?

static IEnumerable<IEnumerable<string>> Subsequences(IEnumerable<string> words) 
    { 
     var arr = words as string[] ?? words.ToArray(); 
     int len = arr.Count(); 
     for (int i = 1; i < len; i++) 
      for (int j = 0; i + j <= len; j++) 
       yield return arr.Skip(j).Take(i); 
    } 

你會如何在F#中編寫它?

回答

2

這裏的邏輯相同的譯文:

let subsequences words = 
    seq { 
     let len = Array.length words 
     for i = 1 to len - 1 do 
      for j = 0 to len - i do 
       yield words.[j..j+i-1] 
    }