-- Columnwise.hs -- Tom Moertel -- CVS $Id: Columnwise.hs,v 1.2 2004/11/12 22:13:13 thor Exp $ -- | This module provides functions for folding over matrix -- data column wise. The primary advantage over transposing -- the matrix to expose the columns is that this method -- requires less space, especially if you are computing a -- single summary value per column. module Columnwise ( foldC, foldC' ) where import Data.List -- | Folds a function over each column of a matrix. (foldC f z rows) -- is equivalent to (map (foldl f z) (transpose rows)) but requires -- less space. -- -- Example: foldC (+) 0 [[0, 1], [2, 3]] ==> [2,4] foldC :: (b -> a -> b) -> b -> [[a]] -> [b] foldC f z rows = foldl (zipWith f) (repeat z) rows -- | Strict version of foldC. foldC' :: (b -> a -> b) -> b -> [[a]] -> [b] foldC' f z rows = foldl' (zipWith f) (repeat z) rows where foldl' f z [] = z foldl' f z (x:xs) = (foldl' f $! lseq (f z x)) xs lseq xs = foldr seq () xs `seq` xs -- ================================================================= -- -- Copyright (C) 2004 Thomas Moertel. -- -- This program is free software; you can redistribute it and/or -- modify it under the terms of the GNU General Public License -- as published by the Free Software Foundation; either version 2 -- of the License, or (at your option) any later version. -- -- The text of the GNU GPL may be found in the LICENSE file, -- included with this software, or online at the following URL: -- -- http://www.gnu.org/copyleft/gpl.html -- -- This program is distributed in the hope that it will be useful, -- but WITHOUT ANY WARRANTY; without even the implied warranty of -- MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the -- GNU General Public License for more details. -- -- Except as provided for under the terms of the GNU GPL, all rights -- are reserved worldwide. -- -- =================================================================