【发布时间】:2015-10-09 07:45:59
【问题描述】:
这是一项家庭作业。我们得到了一个新类型的矩阵,它是教授对抽象矩阵的实现。我的主要问题是如何创建 Matrix 类型的列表。我需要实现的第一个函数 fillWith 采用一个元组,它是要创建的矩阵的(行数、列数)以及要放置在每个索引处的数据。
module Matrix (Matrix, fillWith, fromRule, numRows, numColumns,
at, mtranspose, mmap, add, mult)
where
-- newtype is like "data", but has some efficiency advantages
newtype Matrix a = Mat ((Int,Int),(Int,Int) -> a)
--fillWith :: (Int,Int) -> a -> (Matrix a)
--fillWith ix val = Mat ((,
--trying to create a row of type Matrix
row_matrix :: [a] -> Matrix a
row_matrix ls = Mat ((1, length ls), (\x y -> if x > 1 then undefined else ls !! (y-1)))
【问题讨论】:
-
Matrix的这个定义很糟糕。你可以告诉你的教授我是这么说的。 Unidiomatic,与只是有点奇怪的data Matrix a = Mat { width, height :: Int, members :: (Int, Int) -> a }相比,我对效率获胜的说法提出异议。 -
您可能想问一个关于新类型与数据声明的问题 with the specific definition .