Haskell 中的交换函数
我想在 haskell 中编写一个函数,它不介意我以什么顺序向它提供参数,例如,我想
reproduce1 :: Male -> Female -> Child
reproduce2 :: Female -> Male -> Child
通过一个函数“regenerate”来统一这两个函数。
I want to write a function in haskell which would not mind in what order I provide it its argument, for example, I want to unify these two functions
reproduce1 :: Male -> Female -> Child
reproduce2 :: Female -> Male -> Child
by one function 'reproduce'.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您可以使用多参数类型类来完成此操作。
不过,我很好奇您为什么要这样做。
You can do this using a multi-parameter type class.
However, I'm curious about why you would want to do this.
也许您想将参数打包成数据类型并使用记录 (参见“标记字段”)?
不过,我和@hammar 一样好奇。
Maybe you'd like to package your arguments into a datatype and use records (see "Labelled Fields") instead?
However, I share @hammar's curiosity.
我正在考虑这样的事情,如果两个成年人的性别相同,则会抛出异常:
I was thinking about something like this, which throws an exception if both adults are of the same sex:
我强烈建议修复顺序,先说男性,然后是女性,或者像 ShiDoSi 的解决方案中那样创建“婚姻”数据类型。
但是,请检查论文第 12 页的“会话类型和对偶性”部分 “类型函数的乐趣” - 我认为这是一个很好的例子,您需要以对称方式耦合类型雌雄配对。
I strongly recommend fixing the order, say first Male and then Female, or making a "marriage" datatype as in ShiDoSi's solution.
However, check section "Session types and duality", pg 12 in the paper "Fun with type functions" - I think that is a good example where you need types coupled in symmetric pairs male-female.