Tomas Andersson <[EMAIL PROTECTED]> writes: > You can never go wrong with a good old fashioned hand written tail recursion > when you're in doubt, they are pretty much the closest thing to for-loops > there is in haskell and should be easy to grok for Imperative programmers and > usually produce really fast code. > > sum vect = let d = dim vect > in sum' (d - 1) 0 > where sum' 0 s = s + (vect @> 0) > sum' index s = sum' (index - 1) (s + (vect @> index)) >
Do I need the strict version of sum'? Put something like sum' a b | a `seq` b `seq` False = undefined Or ghc will optimize it automatically? I always don't know when such optimization is useful. > > > I don't know the hmatrix api very well, but if there is a function for > computing the inner product between two vectors you could always do something > like the following meta code: > > sum v = innerProduct v <1,1,1,1,1,1> I just doubt the efficiency here. If v is a very large vector, I guess the allocation time of that intermediate vector [1,1..] is not small. But it is just my guess. Xiao-Yong -- c/* __o/* <\ * (__ */\ < _______________________________________________ Haskell-Cafe mailing list Haskell-Cafe@haskell.org http://www.haskell.org/mailman/listinfo/haskell-cafe