【发布时间】:2017-08-04 17:25:41
【问题描述】:
我想实现能够保证导出一组类似功能的模块。
举个例子:假设我想翻译一个词。每个单词都从源语言(比如English)映射到目标语言(比如Spanish 和Russian)。
我的主应用程序将导入西班牙语和俄语的模型并选择默认模型俄语。我想保证,每个模型都有:
- 一个函数
translateToken :: String -> String - 一个函数
translatePhrase :: String -> String
具体行为在其中实现。
我该怎么做?
编辑,关于李的回答: 如何使用包含使用守卫的函数的记录语法创建数据类型?
-- 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
【问题讨论】:
标签: haskell design-patterns interface