How to resolve the algorithm Dot product step by step in the Haskell programming language

Published on 7 June 2024 03:52 AM

How to resolve the algorithm Dot product step by step in the Haskell programming language

Table of Contents

Problem Statement

Create a function/use an in-built function, to compute the   dot product,   also known as the   scalar product   of two vectors. If possible, make the vectors of arbitrary length.

As an example, compute the dot product of the vectors:

If implementing the dot product of two vectors directly:

Let's start with the solution:

Step by Step solution about How to resolve the algorithm Dot product step by step in the Haskell programming language

There are two functions with same name but different type signatures:

  1. dotp Function:

    • Takes two lists of numeric elements ([a]) and returns a single numeric value (a).
    • Calculates the dot product of the two lists by multiplying corresponding elements and summing the results.
    • Has a strict requirement that the input lists must have the same length, otherwise it generates an error.
  2. dotProduct Function:

    • Also takes two lists of numeric elements ([a]) and returns a Maybe a.
    • Calculates the dot product similarly to dotp, but returns Just with the result if the input lists have the same length, or Nothing if they don't match.

In the main function, two different scenarios are shown:

  • For dotp, the input lists [1, 3, -5] and [4, -2, -1] have the same length, so it calculates the dot product and prints the result, which is 3.

  • For dotProduct, the same input lists are used. Since they still have the same length, it calculates the dot product, stores it in a Maybe, and prints the value wrapped in Just.

The main difference between the two functions is that dotp strictly enforces the same-length requirement and generates an error if it's violated, while dotProduct allows for different-length lists and handles the case gracefully by returning Nothing.

Source code in the haskell programming language

dotp :: Num a => [a] -> [a] -> a 
dotp a b | length a == length b = sum (zipWith (*) a b)
         | otherwise = error "Vector sizes must match"
 
main = print $ dotp [1, 3, -5] [4, -2, -1] -- prints 3


dotProduct :: Num a => [a] -> [a] -> Maybe a
dotProduct a b 
  | length a == length b = Just $ dp a b
  | otherwise = Nothing
    where
      dp x y = sum $ zipWith (*) x y


main :: IO ()
main = print n
  where
    Just n = dotProduct [1, 3, -5] [4, -2, -1]


  

You may also check:How to resolve the algorithm Delegates step by step in the Pascal programming language
You may also check:How to resolve the algorithm Sort stability step by step in the REXX programming language
You may also check:How to resolve the algorithm Hello world/Standard error step by step in the Nu programming language
You may also check:How to resolve the algorithm Determinant and permanent step by step in the Go programming language
You may also check:How to resolve the algorithm Anagrams step by step in the Oforth programming language