[Haskell-cafe] "sum" in hmatrix and blas?

Tomas Andersson toman144 at student.liu.se
Sun Jun 8 08:54:17 EDT 2008


Den Sunday 08 June 2008 00.45.01 skrev Xiao-Yong Jin:

> Any delightful idea to convert my mind from a C shaped one
> to a Haskell shaped one?
>

 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))


 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>


> Best,
> Xiao-Yong




More information about the Haskell-Cafe mailing list