2015-05-14 26 views
2

我想表示一個文件結構以便稍後將其寫入磁盤。我需要的是有一種方式來表示文件,文件夾,他們的關係和他們的內容。例如,代表一個文件結構是這樣的:表示文件/文件夾結構的最佳方式

|-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", []); 
] 

這適用於現在,但我想知道是否有代表一個更聰明的方法像這樣的文件結構。

回答

3

您的表述是完全直接的,因此除非您有更具體的要求,否則很難想象更好的表達方式。

一個微小的(或多或少化妝品)的變化是使用:

type fileTree = 
    | File of string * string 
    | Folder of string * fileTree list 

這是不一樣的類型,更是一點點的有效的,當你不需要的直接表示對。

如果您想在這種結構中快速查看文件,並且結構將變得非常大,則可能需要使用嵌套地圖或散列表。

更新

有兩種類型之間的差異進行了一些討論。在此之前已經很好地解釋過,但是我一直無法找到該頁面。這裏的顯示差異會話:

$ ocaml 
     OCaml version 4.02.1 

# type f = A of (int * int);; 
type f = A of (int * int) 
# type g = B of int * int;; 
type g = B of int * int 
# let x = (8, 7);; 
val x : int * int = (8, 7) 
# A x;; 
- : f = A (8, 7) 
# B x;; 
Error: The constructor B expects 2 argument(s), 
     but is applied here to 1 argument(s) 
# 

A構造函數有一個值,一對整數的。 B構造函數接受兩個獨立的int值,而不是一對。

在內存中,類型爲f的值將具有標題和一個字段。一個字段將指向一對(一個標題和兩個字段)。類型g的值將只有一個標題和兩個字段。

這不是什麼大不了的事情,大多隻是有趣的(至少對我來說)。

更新2

這裏的這個問題很好,所以討論:

int * int vs (int * int) in OCaml sum type

+0

能否請您解釋這方面你的類型是不同的,有些更有效? – Romildo

+0

本質上'文件(字符串*字符串)'有一個字段是一對,而'文件的字符串*字符串'有兩個單獨的字段。從一對中檢索一個元素花費的時間稍長一些,因爲還有一個額外的間接。 –

+0

記錄在哪裏?我在手冊中找不到它。 – Romildo