diff options
| author | Steven Wilson <steven1170@zoho.eu> | 2022-01-03 16:51:40 +0000 |
|---|---|---|
| committer | Steven Wilson <steven1170@zoho.eu> | 2022-01-03 16:51:40 +0000 |
| commit | 71db3413b752c37ccd910aab43422720e447818c (patch) | |
| tree | b1afca1dba27f5766dc1bebe7f3399c9743cf346 | |
| parent | 41f8dae6667e8b4215b9f2507d7a2e14236890b9 (diff) | |
| download | perlweeklychallenge-club-71db3413b752c37ccd910aab43422720e447818c.tar.gz perlweeklychallenge-club-71db3413b752c37ccd910aab43422720e447818c.tar.bz2 perlweeklychallenge-club-71db3413b752c37ccd910aab43422720e447818c.zip | |
add solutions week 145 task 1 in perl and haskell
| -rw-r--r-- | challenge-145/steven-wilson/haskell/ch-1.hs | 3 | ||||
| -rw-r--r-- | challenge-145/steven-wilson/perl/ch-1.pl | 26 |
2 files changed, 29 insertions, 0 deletions
diff --git a/challenge-145/steven-wilson/haskell/ch-1.hs b/challenge-145/steven-wilson/haskell/ch-1.hs new file mode 100644 index 0000000000..07c9912f18 --- /dev/null +++ b/challenge-145/steven-wilson/haskell/ch-1.hs @@ -0,0 +1,3 @@ +dot_product :: [Int] -> [Int] -> Int +dot_product [x] [y] = x * y +dot_product (x:xs) (y:ys) = (x * y) + dot_product (xs) (ys) diff --git a/challenge-145/steven-wilson/perl/ch-1.pl b/challenge-145/steven-wilson/perl/ch-1.pl new file mode 100644 index 0000000000..b564f8becb --- /dev/null +++ b/challenge-145/steven-wilson/perl/ch-1.pl @@ -0,0 +1,26 @@ +#!/usr/bin/env perl +# Week 145 Task 1 +# Dot Product + +use strict; +use warnings; +use feature qw/ say /; + +my @a = ( 1, 2, 3 ); +my @b = ( 4, 5, 6 ); + +# $dot_product = (1 * 4) + (2 * 5) + (3 * 6) => 4 + 10 + 18 => 32 + +say dot_product( \@a, \@b ); + +sub dot_product { + my ( $a_ref, $b_ref ) = @_; + my @a = @{$a_ref}; + my @b = @{$b_ref}; + my $len = scalar @a; + my $sum = 0; + for ( 0 .. ( $len - 1 ) ) { + $sum += ( $a[$_] * $b[$_] ); + } + return $sum; +} |
