Dot product: Difference between revisions
m
→{{header|Haskell}}: Tidied the Maybe version a little
(Dot product in True BASIC) |
m (→{{header|Haskell}}: Tidied the Maybe version a little) |
||
Line 1,458:
Or, using the Maybe monad to avoid exceptions and keep things composable:
<syntaxhighlight lang="haskell">
dotProduct a b
| length a == length b = Just $ sum (zipWith (*) a b)▼
| otherwise = Nothing
where
main :: IO ()▼
▲main :: IO ()
main = print n
where
Just n = dotProduct [1, 3, -5] [4, -2, -1]</syntaxhighlight>
=={{header|Hoon}}==
|