2016-11-20 62 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