Created
August 16, 2013 23:51
-
-
Save reuk/6254492 to your computer and use it in GitHub Desktop.
mucking around with applicative functors
I have literally no idea what I'm doing
function compositions are well shiny though
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
module Matrix where | |
import Control.Applicative | |
import Data.List | |
data Matrix a = Matrix [[a]] | |
deriving (Eq, Show) | |
instance Functor Matrix where | |
fmap f (Matrix x) = Matrix $ fmap (fmap f) x | |
instance Applicative Matrix where | |
pure x = Matrix [[x]] | |
(Matrix f) <*> (Matrix x) = Matrix (zipWith (zipWith (\ f x -> f x)) f x) | |
instance Num a => Num (Matrix a) where | |
(+) = (<*>) . (<$>) (+) | |
(-) = (<*>) . (<$>) (-) | |
abs = fmap abs | |
signum = fmap signum | |
fromInteger = pure . fromInteger | |
(*) (Matrix a) (Matrix b) = Matrix [[sum $ zipWith (*) x y | y <- (transpose b)] | x <- a] |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment