获取无效驱动器号的列表

时间:2012-04-11 21:52:06

标签: f# sequences

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 one list from another list,但无法将其应用于我的问题,因为我看到的解决方案似乎都没有编译。 List.Contains不存在(缺少引用?)和ListA - ListB也不编译。

2 个答案:

答案 0 :(得分:7)

open System.IO
let driveLetters = set [ for d in DriveInfo.GetDrives() -> d.Name.[0] ]
let unused = set ['A'..'Z'] - driveLetters

答案 1 :(得分:3)

您的第一个错误是在charstring之间混合,最好从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)