haskelldot-product

Haskell adding Vectors and dot product


the tasks are:

  1. Write a function that adds vectors
  2. A function that calculates the dot product
  3. A function that can use the dot product to determine whether the angle is right.

My approaches:

  1. .

    addVectors:: (Num a) => a -> a -> a
    addVectors (a1, a2, a3) (b1, b2, b3) = (a1 + b1) (a2 + b2) (a3 + b3)
    
  2. .

    scalarProduct:: (Num a) => a -> a -> a 
    scalarProduct (a1, a2, a3) (b1, b2, b3) = (a1 * b1) (a2 * b2) (a3 * b3)
    
  3. ?

So I'm not sure how to phrase it. In the end, true should come out. This is likely to happen if the dot product equals 0.

I tried approaching tasks 1 and 2 myself.


Solution

  • Everyone is dancing around this, but I'm going to be explicit.

    addVectors (a1, a2, a3) (b1, b2, b3) = (a1 + b1) (a2 + b2) (a3 + b3)
    

    Your output is not valid tuple syntax.

    You must instead write

    addVectors (a1, a2, a3) (b1, b2, b3) = (a1 + b1, a2 + b2, a3 + b3)