繁体   English   中英

F#列出文件夹和文件

[英]F# list folders and files

我必须编写一个名为“ find”的函数,在其中可以给我一个Name作为输入。 该功能必须搜索具有给定名称的文件或文件夹,并必须返回列表中的文件或文件夹。

在这里,您将看到一个示例,其外观如下:

find "Hallo" hallo = []
find "Hallo.txt" hallo = [["Hallo.txt"]]
find "Hallo.txt" dokumente = [["Dokumente"; "Hallo.txt"]]
find "Hallo.txt" (Folder ("Test", [hallo; dokumente ])) =
[["Test"; "Hallo.txt"]; ["Test"; "Dokumente"; "Hallo.txt"]]

那就是我到目前为止尝试过的:

type Node =
    | File   of string * Nat          
    | Folder of string * (Node list)

let rec find (name: string) (root: Node): string list list =
        match root with
        | File (N,G)  ->if N=name then [[N]] else find(name)(root)
        | Folder(N,G) ->if N=name then [[N]] else find(name)(root)
let find name root =
    let rec loop name node acc =
        [ match node with
          | File (n) -> if n = name then yield List.rev (n::acc)
          | Folder (n, l) -> for x in l do yield! loop name x (n::acc) ]
    loop name root []

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM