Matrix multiplication: Difference between revisions
→{{header|AppleScript}}: Normalised argument sequences for map and reduce
m (added a ;Task: (bold) header.) |
(→{{header|AppleScript}}: Normalised argument sequences for map and reduce) |
||
Line 368:
{{trans|JavaScript}}
<lang AppleScript>-- matrixMultiply :: [[n]] -> [[n]] -> [[n]]
to matrixMultiply(a, b)
script mf
Line 375 ⟶ 374:
to lambdaRow(aRow)
set mf to mf of my closure
map(
end lambdaRow
to lambdaCol(bCol)
dotProduct(aRow of my closure
end lambdaCol
end script
map(
end matrixMultiply
-- TEST
on run
matrixMultiply({¬
{-1, 1, 4}, ¬
Line 405 ⟶ 406:
-- GENERIC LIBRARY FUNCTIONS
-- dotProduct :: [n] -> [n] -> Maybe n
on dotProduct(xs, ys)
script mf
on product(a, b)▼
a * b▼
end product▼
on add(a, b)▼
a + b
end add▼
on sum(xs)▼
end sum▼
end script
if length of xs is not length of ys then return missing value
sum(zipWith(
end dotProduct
-- transpose :: [[a]] -> [[a]]
on transpose(xss)
script mf
on lambdaCol(_, iCol)
map(mClosure(my closure's
end lambdaCol
Line 425 ⟶ 440:
end lambdaRow
end script
end transpose
-- zipWith :: (a -> b -> c) -> [a] -> [b] -> [c]
on zipWith(f, xs, ys)
set lng to length of xs
Line 442 ⟶ 457:
end zipWith
-- map :: [a] -> (a -> b) -> [b]
on map(
set mf to mReturn(f)
set lng to length of xs
Line 453 ⟶ 468:
end map
--
on reduce(
set mf to mReturn(f)
Line 464 ⟶ 479:
return v
end reduce
▲-- n -> n -> n
▲on product(a, b)
▲ a * b
▲end product
▲on add(a, b)
▲ a + b
▲end add
▲on sum(xs)
▲ reduce(xs, add, 0)
▲end sum
|