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">dotpdotProduct :: Num a => [a] -> [a] -> Maybe a
dotProduct a b
:: Num a
=>| length [a] ->== [a]length ->b Maybe= Just $ dp a b
dotp a b
| length a == length b = Just $ sum (zipWith (*) a b)
| otherwise = Nothing
where
| length a == lengthdp bx y = Justsum $ sum (zipWith (*) ax b)y
 
main :: IO ()
main = mbPrint $ dotp [1, 3, -5] [4, -2, -1] -- prints 3
 
main :: IO ()
mbPrint
main = print n
:: Show a
where
=> Maybe a -> IO ()
Just n = dotProduct [1, 3, -5] [4, -2, -1]</syntaxhighlight>
mbPrint (Just x) = print x
mbPrint n = print n</syntaxhighlight>
 
=={{header|Hoon}}==
9,655

edits