3 ms·
Worth noting that the basic functions like “sum”, “transpose”, and “foldr1” are already available in the standard library in Haskell. Dunno why they weren’t use
by evincarofautumn 8y ago
Worth noting that the basic functions like “sum”, “transpose”, and “foldr1” are already available in the standard library in Haskell. Dunno why they weren’t used—just a learning exercise?
It’s often a good strategy to arrive at a simple definition by starting with a list comprehension or do-notation, then desugaring & simplifying it to use Functor or Applicative operators if it makes things nicer. That’s a good way to build experience with using those functions and develop a sense of when to use sugary notation and when not to.
For example, if I were coming from APL and in a pointfreeish mood, I might’ve rewritten the list comprehension version toward the end like this:
generalMatrixProduct add mul a b
= (<$> transpose b) . innerProduct <$> a
where innerProduct = fmap (foldr1 add) . zipWith mul
Which reads as “The inner product of each row of ‘a’ with every column (a.k.a. row of the transpose) of ‘b’.”
(In reality I’d probably just stick to do-notation, though, unless I found a particularly nice fully pointfree version, and also avoid foldr1 since it’s partial.)