{-# LANGUAGE NoMonomorphismRestriction #-}
import Data.List
-- Generalize "foldr1 max" for mat1 of 1vec, mat2 of 2vec, mat3 of 3vec
-- Leftmost coordinate of vec corresponds to inner list.
-- 1D case: [[1],[2],.....]
-- Need a generic way of mapping "max" over the proper element of
-- lists.
-- Map functions over nth component of list
liftC1 f n as = f (as !! n)
liftC2 f n as bs = f (as !! n) (bs !! n)
-- Fold over rows, columns, ... of a tensor. This reduces rank n to n-1.
-- When using list of list representations, the simplest approach
-- seems to be to just fold the bottom dimenions, since it's
-- map $ map $ ... $ fold
-- The problem is then to encode transpositions.
-- However... maybe some combination of map $ map $ fold $ map also
-- works? Trouble is that this is a 2-1 op, so it needs a lift2
-- operation..
-- Anways, let's stick to 2D. Then it's a combination of inner fold,
-- transpose (only 1 possibility), and possibly cross product to
-- combine folds, i.e. for matrix bbox.
-- stick to 2D:
-- inner fold
foldri f = map $ foldr f
foldri1 f = map $ foldr1 f
m = [[11,12,13],[21,22,23],[31,32,33]]
t1 = (foldr1 max) m
t2 = (foldr1 max . transpose) m
-- transpose -> import Data.List
-- cross product
cross' f xs ys = map fy ys where
fy y = map (\x -> f x y) xs
cross f xs ys = map (\y -> map (\x -> f x y) xs) ys
-- tx = cross (+) [1,2,3] [10,20,30]
mapMatrix f = map (map f)
tx' = mapMatrix (1+) m