2016-11-20 37 views
0

HTML結構命名策略考慮以下Haskell的類型,描述一個HTML文檔的基本結構:用於描述在Haskell

data HTML    = HTML HTMLElement [HTMLElement] 
data HTMLElement  = HTMLElement HTMLElementTagname [HTMLAttribute] 
data HTMLElementTagname = HTMLElementTagname String 
data HTMLAttribute  = HTMLAttribute (HTMLAttributeKey, HTMLAttributeValue) 
data HTMLAttributeKey = HTMLAttributeKey String 
data HTMLAttributeValue = HTMLAttributeValue String 

我的問題主要是關於這個結構的適當的命名策略。在最後一個類型HTMLAttributeValue中查找示例,它清楚地顯示了定義類型的層次結構,但僅使用了我正在使用的駝峯式約定。沒有人會讓別人稱之爲value_of_html_attribute,只是爲了舉一個例子。這可能被認爲是有問題的。

另一個命名看起來是這樣的:

data HTML  = HTML HTML [Element] 
data Element = Element Tagname [Attribute] 
data Tagname = Tagname String 
data Attribute = Attribute (Key, Value) 
data Key  = Key String 
data Value  = Value String 

然而,這會污染全局命名空間。例如,KeyValue的類型可能適用於其他數據結構的代碼中的其他位置。然而,後面的例子對我來說看起來更具可讀性,而第一個例子似乎相當迂腐。

你將如何實現這一點?

回答

1

在類型名稱中反映完整的層次結構(如第一個示例中所示)似乎過多。至於第二個例子,如果命名污染最終會被證明是有問題的,合格的進口是一個簡單的解決方法:

module FooBar where -- etc. 

data HTML  = HTML HTML [Element] 
data Element = Element Tagname [Attribute] 
data Tagname = Tagname String 
data Attribute = Attribute (Key, Value) 
data Key  = Key String 
data Value  = Value String 
import qualified FooBar as H 

someHtml :: H.HTML 
someHtml = -- etc. 

一些相關的現有技術:blaze-markupthreepenny-gui