用于在字符串中查找单词的简单代码

时间:2011-04-03 03:50:35

标签: f#

我从书中尝试这个代码但得到错误,有人可以帮助我吗?

#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编译器说:

  

存在多种类型,称为“Set”,采用不同数量的通用参数。提供类型实例化以消除类型解析的歧义,例如'设置< _>'。

这是什么意思?

2 个答案:

答案 0 :(得分: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)

答案 1 :(得分:0)

我认为你应该尝试像

这样的东西
Set<string>.Create()

定义将存储在集合中的数据类型。