chapter 3 finished

This commit is contained in:
nora 2021-07-09 11:36:37 +02:00
parent c43a16ecde
commit 9c41b5059e
2 changed files with 39 additions and 0 deletions

30
chapter3/Matrix.idr Normal file
View file

@ -0,0 +1,30 @@
module Matrix
import Data.Vect
addMatrix : Num numType =>
Vect rows (Vect cols numType) ->
Vect rows (Vect cols numType) ->
Vect rows (Vect cols numType)
addMatrix = zipWith (zipWith (+))
multMatrix : Num numType =>
Vect n (Vect m numType) -> Vect m (Vect p numType) ->
Vect n (Vect p numType)
-- 😔
createEmpties : Vect n (Vect 0 elem)
createEmpties = replicate _ []
createEmpties2 : Vect n (Vect 0 elem)
createEmpties2 {n = Z} = []
createEmpties2 {n = (S k)} = [] :: createEmpties2
transHelper : (x : Vect n elem) -> (xsTrans : Vect n (Vect len elem)) -> Vect n (Vect (S len) elem)
transHelper = zipWith (\x, y => x :: y)
transposeMat : Vect m (Vect n elem) -> Vect n (Vect m elem)
transposeMat [] = createEmpties
transposeMat (x :: xs) = let xsTrans = transposeMat xs in
transHelper x xsTrans

View file

@ -52,3 +52,12 @@ my_map f (x :: xs) = (f x) :: my_map f xs
my_vect_map : (a -> b) -> Vect n a -> Vect n b
my_vect_map _ [] = []
my_vect_map f (x :: xs) = (f x) :: my_vect_map f xs
lengthLong : Vect n elem -> Nat
lengthLong [] = 0
lengthLong (x :: xs) = S (lengthLong xs)
lengthLong2 : Vect n elem -> Nat
lengthLong2 {n} xs = n