RabbitFarm

2019-04-14

Perl Weekly Challenge 003

Both parts of this week's challenge seemed to have the same theme of performing a calculation that inspires recursion. I wrote a bit about recursion in Perl last week so there isn't too much new to add on that subject in describing the code.

The problem statements are short and are included in the first comment of the code. The code blocks shown link to GitHub Gists.

Part 1

This a pretty straightforward solution to the stated problem. To jazz things up a little I used currying to generate the three is_divisible_by_X functions. This is neat to see in practice, but is never required. In fact, I've never seen currying used seriously in Perl. Just these sorts of toy problems and examples. If anyone is interested in why someone would want to do this I refer you to a nice Perlmonks post on why currying may have some practical usage in other programming environments.

I generally avoid using any CPAN modules for these problem sets but I made a small exception in the code for Part 1 and use boolean; this module simply allows the use of true and false as boolean values. Completely unnecessary, of course, but I have always preferred the aesthetics of boolean keywords.

Part 2

The computation for Pascal's Triangle (binomial co-efficients) is, similar to the Hamming Numbers of Part 1, straightforward. This week perhaps my creativite energy was a bit low. The solution for Part 2 is about as vanilla as you can get!


posted at: 11:09 by: Adam Russell | path: /perl | permanent link to this entry