Skip to content

Instantly share code, notes, and snippets.

@reuk
Created August 16, 2013 23:51
Show Gist options
  • Save reuk/6254492 to your computer and use it in GitHub Desktop.
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
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