Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Commutative function in Haskell

Tags:

haskell

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'.

like image 799
Dilawar Avatar asked Oct 03 '11 16:10

Dilawar


1 Answers

You can do this using a multi-parameter type class.

{-# 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

However, I'm curious about why you would want to do this.

like image 50
hammar Avatar answered Oct 06 '22 17:10

hammar