haskell - Haskell中的交换函数

标签 haskell

我想在haskell中编写一个函数,它不介意我以什么顺序提供它的参数,例如,我想统一这两个函数

    reproduce1 :: Male -> Female -> Child
    reproduce2 :: Female -> Male -> Child

通过一个功能“再现”。

最佳答案

您可以使用多参数类型类来执行此操作。

{-# LANGUAGE MultiParamTypeClasses #-}

class Reproduce x y where
  reproduce :: x -> y -> Child

instance Reproduce Male Female where
  reproduce = reproduce1

instance Reproduce Female Male where
  reproduce = reproduce2

但是,我很好奇您为什么要这样做。

关于haskell - Haskell中的交换函数,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7637906/

相关文章:

sorting - 通用排序功能的最通用可能/有意义的签名

haskell - 如何命名物理学中大写的 Haskell 变量

haskell - 是 (map f) == concatMap (map f . ( :[]))?

unit-testing - 模拟 IO 操作 : getArgs and putStrLn

haskell - 使用 MonadIO 测试类型类 : "No instance nor default method" error

haskell - Cabal 在 Windows 上安装 PCRE-Light。 Extra-include-dirs/extra-lib-dirs 不起作用

haskell - 使用列表理解代替递归

haskell - 水平、垂直和对角线上的数字相乘

c++ - 将 Haskell ByteString 转换为 C++ std::string

haskell - 这个listArray是如何填充的?