1
let private GetDrives = seq{
let all=System.IO.DriveInfo.GetDrives()
for d in all do
    //if(d.IsReady && d.DriveType=System.IO.DriveType.Fixed) then
        yield d
}

let valid={'A'..'Z'}
let rec SearchRegistryForInvalidDrive (start:RegistryKey) = seq{
    let validDrives=GetDrives |> Seq.map (fun x -> x.Name.Substring(0,1))
    let invalidDrives= Seq.toList validDrives |> List.filter(fun x-> not (List.exists2 x b)) //(List.exists is the wrong method I think, but it doesn't compile

我遵循F#: Filter items found in a list from another list但无法将其应用于我的问题,因为我看到的两种解决方案似乎都无法编译。List.Contains 不存在(缺少参考?)并且 ListA - ListB 也不编译。

4

2 回答 2

7
open System.IO
let driveLetters = set [ for d in DriveInfo.GetDrives() -> d.Name.[0] ]
let unused = set ['A'..'Z'] - driveLetters
于 2012-04-11T21:56:31.927 回答
3

您的第一个错误是在char和之间混合string,最好从以下开始char

let all = {'A'..'Z'}
let validDrives = GetDrives |> Seq.map (fun x -> x.Name.[0])

现在无效的驱动器号是那些在all但不在的字母validDrives

let invalidDrives = 
      all |> Seq.filter (fun c -> validDrives |> List.forall ((<>) c))

由于validDrives经过多次遍历以检查成员资格,因此在此示例中将其转换为集合更好:

let all = {'A'..'Z'}
let validDrives = GetDrives |> Seq.map (fun x -> x.Name.[0]) |> Set.ofSeq
let invalidDrives = all |> Seq.filter (not << validDrives.Contains)
于 2012-04-11T22:05:44.780 回答