簡體   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