我想表示一個文件結構以便稍後將其寫入磁盤。我需要的是有一種方式來表示文件,文件夾,他們的關係和他們的內容。例如,代表一個文件結構是這樣的:表示文件/文件夾結構的最佳方式
|-one.txt -> contains "This is file 1"
|-aFolder
| |-aNestedFolder
| | |-aDoublyNestedEmptyFolder
|-anotherFolder
| |-aNestedFile -> contains "Contents of aNestedFile"
|-yetAnotherEmptyFolder
我現在用的這個權利:
type fileTree =
| File of (string * string)
| Folder of (string * (fileTree list))
let example = [
File ("one.txt", "This is file 1");
Folder ("aFolder",
[Folder ("aNestedFolder",
[Folder ("aDoublyNestedEmptyFolder", [])])]) ;
Folder ("anotherFolder",
[File ("aNestedFile", "Contents of aNestedFile")]);
Folder ("yetAnotherEmptyFolder", []);
]
這適用於現在,但我想知道是否有代表一個更聰明的方法像這樣的文件結構。
能否請您解釋這方面你的類型是不同的,有些更有效? – Romildo
本質上'文件(字符串*字符串)'有一個字段是一對,而'文件的字符串*字符串'有兩個單獨的字段。從一對中檢索一個元素花費的時間稍長一些,因爲還有一個額外的間接。 –
記錄在哪裏?我在手冊中找不到它。 – Romildo