pyCthon
pyCthon

Reputation: 12361

writing a function that accepts multiple input and has multiple outputs in haskell

whats the correct way to write a function that can accept different input's and have different outputs

for example i'm using hmatrix and lets say i want to accept a Matrix or a Vector in my function, and the output can be a Matrix or a Vector depending on hte formula where T in the below example can be a matrix or a vector , is maybe the right tool for this?

Myfunc ::(Matrix A, Matrix/Vector T) -> Maybe(Matrix/Vector T)

Update using either mentioned below here is one possible solution

Myfunc :: Maybe Matrix Double t -> (Either Vector Double a,Matrix Double a) -> Either (Matrix Double T,Vector Double T) 

Upvotes: 1

Views: 628

Answers (4)

Riccardo T.
Riccardo T.

Reputation: 8937

You could either have a look at Either (I know, it's a bad joke), or, if your function has a general meaning but different implementations on different data types, you could define a typeclass.

edit: I didn't add any further details because your question isn't completely clear to me

Upvotes: 2

Martinsos
Martinsos

Reputation: 1693

The question is what do you want to do with your input? For example if you want to do comparison then you can say input has to be of class Ord like this:

myFunc :: (Ord a) => a -> b

Another way would be to use Either, but in that case you can have only two different data types. For example

myFunc :: Either a b -> Either c d

can accept and return different types.

Upvotes: 1

Chris Taylor
Chris Taylor

Reputation: 47402

Take a look at how matrix multiplication and left-divide are implemented in the source code for HMatrix.

Essentially, they define a multi-parameter type class which tells the function how to behave for different inputs, and it has a functional dependency which tells it what output is appropriate. For example, for multiplication:

{-# LANGUAGE MultiParamTypeClasses  #-}
{-# LANGUAGE FunctionalDependencies #-}

-- |The class declaration 'Mul a b c' means "an 'a' multiplied by a 'b' returns
--  a 'c'". The functional dependency 'a b -> c' means that once 'a' and 'b' are
--  specified, 'c' is determined.
class Mul a b c | a b -> c where
    -- | Matrix-matrix, matrix-vector, and vector-matrix products.
    (<>)  :: Product t => a t -> b t -> c t

-- |Matrix times matrix is another matrix, implemented using the matrix
--  multiplication function mXm
instance Mul Matrix Matrix Matrix where
    (<>) = mXm

-- |Matrix times vector is a vector. The implementation converts the vector
--  to a matrix and uses the <> instance for matrix/matrix multiplication/
instance Mul Matrix Vector Vector where
    (<>) m v = flatten $ m <> asColumn v

-- |Vector times matrix is a (row) vector.
instance Mul Vector Matrix Vector where
    (<>) v m = flatten $ asRow v <> m

Upvotes: 4

brano
brano

Reputation: 2870

Another solution would be to use a list of lists [[a]]. Essentially a vector is a matrix with a single row.

Upvotes: -2

Related Questions