Dot product (Haskell)

From LiteratePrograms
Jump to: navigation, search
Other implementations: Haskell | Python

[edit] Recursion

<<vector_cross_product_recursion.hs>>=
cross :: (Fractional a) => [a] -> [a] -> [a]
cross [] [] = 0
cross (u:us) (v:vs) = (u * v) + cross us vs

[edit] List Comprehension

<<vector_addition_list_comprehension.hs>>=
cross :: (Fractional a) => [a] -> [a] -> [a]
cross us vs = sum [ u * v | (u, v) <- zip us vs ]

[edit] Higher order

<<vector_cross_product_higher_order_map2.hs>>=
cross :: (Fractional a) => [a] -> [a] -> [a]
cross us vs = sum (zipWith (*) us vs)
Download code
hijacker
hijacker
hijacker
hijacker