aboutsummaryrefslogtreecommitdiff
path: root/challenge-145/luca-ferrari/postgresql/ch-1.sql
blob: bfa9121355f0a8824f0f2cf86f139ab83e212cd2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
/*
testdb=> select f_dot_product( array[1,2,3], array[4,5,6] );
f_dot_product
---------------
32
*/
CREATE OR REPLACE FUNCTION
f_dot_product( a int[], b int[] )
RETURNS int
AS
$CODE$
DECLARE
        i int;
        total int := 0;
BEGIN
        FOR i IN 1 .. array_length( a, 1 ) LOOP
            total := total + a[ i ] * b[ i ];
        END LOOP;

        RETURN total;
END
$CODE$
LANGUAGE plpgsql;