2011-04-03 106 views
0

我從書中嘗試此代碼但遇到錯誤,有人可以幫我嗎?查找字符串中的單詞的簡單代碼

#if INTERACTIVE 
#r "FSharp.PowerPack.dll";; 
#r "FSharp.PowerPack.Compatibility.dll";; 
#endif 

open System 
open System.IO 
open System.Collections.Generic 
open Microsoft.FSharp.Collections.Tagged 

let str = "This is a string, try to break it buddy!\nfind!" 

let isWord (words) = 
    let wordTable = Set.Create(words) 
    fun w -> wordTable.Contains(w) 

Set.Create編譯器說:

多種類型存在所謂的 '設置',以通用參數的不同的號碼。提供一個類型實例化來消除類型分辨率,例如'設置< _>'。

這是什麼意思?

+5

標題是一個*小*誤導.. – Mehrdad 2011-04-03 05:37:13

回答

0

我想你應該嘗試像

Set<string>.Create() 

定義將被存儲在集中的數據類型。

3

問題是有兩種類型命名爲Set:F#的一個和PowerPack中的一個(實際上有三種類型,但沒關係)。在你的情況下,F#Set類型會做得很好,所以你不需要PowerPack中的標籤Set。請嘗試以下代碼:

open System 

let str = "This is a string, try to break it buddy!\nfind!" 

let isWord words = 
    let wordTable = new Set(words) // or you can use Set.ofArray words 
    fun w -> wordTable.Contains(w)