use strict; { package Calculator; use List::Util qw/reduce/; sub add { reduce { $a + $b } @_ } sub multiply { reduce { $a * $b } @_ } } { package main; use Test::More tests => 2; # Calculate the fifth triangular number is( Calculator->add(1 .. 5), 15, ); # Calculate five factorial is( Calculator->multiply(1 .. 5), 120, ); }
|
|---|
| Replies are listed 'Best First'. | |
|---|---|
|
Re: Spot the deliberate mistake - list-based maths
by BrowserUk (Patriarch) on Apr 26, 2012 at 17:52 UTC | |
|
Re: Spot the deliberate mistake - list-based maths
by Tanktalus (Canon) on Apr 26, 2012 at 22:09 UTC | |
|
Re: Spot the deliberate mistake - list-based maths
by choroba (Cardinal) on Apr 26, 2012 at 21:54 UTC |