在Haskell中写这个Scala矩阵乘法的冗长讨论后,我想知道......类型安全矩阵乘法会是什么样的?所以这是你的挑战:要么链接到Haskell实现,要么自己实现,如下:
data Matrix ... = ...
matrixMult :: Matrix ... -> Matrix ... -> Matrix ...
matrixMult ... = ...
Run Code Online (Sandbox Code Playgroud)
当matrixMult产生一个错误类型在编译的时候,如果你试图乘两个matricies不兼容的尺寸.布朗尼指出,如果你链接到讨论这个精确主题的论文或书籍,和/或讨论自己这个功能是多么有用/无用.
所以我的情况与此(非常简化)代码非常相似:
import Data.Maybe
import Data.List
data Container a = Container [a]
-- Assumption: an Element can only obtained from a Container. The operation
-- guarentees the Element is in the Container.
data Element a = Element (Container a) a
-- This operation is only valid for Elements that have the same Container.
before :: Eq a => Element a -> Element a -> Bool
before (Element (Container xs) x) (Element (Container ys) y)
| xs == ys = fromJust (elemIndex …Run Code Online (Sandbox Code Playgroud)