F# 在字符串中查找单词的简单代码
我尝试了书中的代码,但出现了一个错误,有人能帮我吗F# 在字符串中查找单词的简单代码,f#,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,
#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”的多个类型,使用不同数量的泛型参数。提供类型实例化以消除类型解析的歧义,例如“Set”
这是什么意思?我想你应该试试这样的东西
Set<string>.Create()
Set.Create()
定义将存储在集合中的数据类型。我认为您应该尝试以下方法
Set<string>.Create()
Set.Create()
定义将存储在集合中的数据类型。问题在于有两种类型名为
set
:F#的一种和PowerPack中的一种(实际上有三种类型,但这并不重要)。在您的情况下,F#集
类型可以很好地工作,因此您不需要从PowerPack中获得标记的集
。请尝试以下代码:
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)
问题是有两种类型,分别命名为
Set
:F#的一种和PowerPack中的一种(实际上有三种类型,但这并不重要)。在您的情况下,F#集
类型可以很好地工作,因此您不需要从PowerPack中获得标记的集
。请尝试以下代码:
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)
这个标题有点误导人这个标题有点误导人。。