ink*_*rap 4 haskell design-patterns interface
我想实现保证导出类似功能集的模块.
为了举个例子:假设我想翻译一个单词.每一个字从源语言映射(比方说English
)目标语言(比方说,Spanish
和Russian
).
我的主要应用程序将导入西班牙语和俄语的模型,并选择默认模型,俄语.我想保证,每个型号都有:
translateToken :: String -> String
translatePhrase :: String -> String
其中实现了特定的行为.
我该怎么做呢?
编辑,关于Lee的答案: 如何使用包含使用防护的功能的记录语法创建数据类型?
-- let's suppose I want to use a function with guards in a record.
-- how can and should i define that?
data Model = Model { translateToken :: String -> String}
-- idea 1) should I define the functions separately, like this?
-- how do I do this in a way that does not clutter the module?
f l
| l == "foo" = "bar"
main :: IO ()
main = print $ translateToken x "foo"
where
x = Model {translateToken=f}
-- idea 2) define the function when creating the record,
-- despite the syntax error, this seems messy:
-- x = Model {name=(f l | l == "foo" = "bar")}
-- idea 3) update the record later
Run Code Online (Sandbox Code Playgroud)
Lee*_*Lee 15
您可以创建包含所需功能的类型,例如
data Model = Model { translateToken :: String -> String,
translatePhrase :: String -> String }
Run Code Online (Sandbox Code Playgroud)
然后为西班牙语和俄语创造价值.