2
我已經寫了一個簡單的類型安全的線性代數庫安全地從一個矩陣
data Natural where
Zero :: Natural
Succ :: Natural -> Natural
type One = Succ Zero
type Two = Succ One
type Three = Succ Two
type Four = Succ Three
data Vector n e where
EmptyVector :: Vector Zero e
(:>) :: e -> Vector n e -> Vector (Succ n) e
deriving instance Eq e => Eq (Vector n e)
deriving instance Show e => Show (Vector n e)
infixr :>
data Matrix r c e where
EmptyMatrix :: Matrix Zero c e
(:/) :: Vector c e -> Matrix r c e -> Matrix (Succ r) c e
deriving instance Eq e => Eq (Matrix r c e)
deriving instance Show e => Show (Matrix r c e)
infixr :/
我現在試圖實現的功能如下
dropRow :: Int -> Matrix r c e -> Matrix r c e
但是,刪除一行,而我的當矩陣的返回值具有不同行數時,部分實現開始拋出錯誤
dropRow i m | i <= 0 = m
dropRow _ EmptyMatrix = EmptyMatrix
有沒有辦法保持我的類型,仍然實現這個功能?我會怎麼做?
太棒了,謝謝!我不知道我必須解釋這兩個'n + 1'情況,但這是有道理的。 – sdasdadas
我沒有意識到它,要麼直到我盯着類型錯誤的一點:-) –