Perl Weekly Challenge 109 in Perl and Elm
Published: 2021-07-27 10:41:05

---
title: Perl Weekly Challenge 109 in Perl and Elm
published: true
date: 2021-04-24 00:00:00 UTC
tags: 
canonical_url: http://perl.kiwi/tales/2021/04/24/perl-weekly-challenge-109-in-perl-and-elm/
---

This week I tried a new OBS setup to livecode the solution on [Twitch.tv/lancew](https://www.twitch.tv/videos/993535075). The setup is thanks to Martin Wimpress who shared his setup ( [Creating the Slim.AI OBS Studio set](https://www.twitch.tv/videos/987350520) ) and this broadcast is extra special as I was able to bring [Mohammad Anwar](http://www.manwar.org/) into the live stream; which was awesome.

This week I solved the "Chowla Numbers" task of the [Perl Weekly Challenge](https://perlweeklychallenge.org/blog/perl-weekly-challenge-109/), first in Perl; then in Elm.

Both I did as TDD-ish as I could, some cheating where I just Data::Dumper'd to STDOUT in the Perl version. The Elm one, I used the tests a bit more as I am less familiar in the language, and to be frank I didn't know an easy way to dump to the screen. ;-)

The Elm version I ended up with this:

```
module Chowla exposing (chowla, n, nums)

import List

nums : Int -> List Int
nums max =
    List.range 1 max
        |> List.map n

n : Int -> Int
n num =
    let
        denominators =
            List.range 2 (num - 1)

        numbers =
            List.map (chowla num) denominators
    in
    List.foldl (+) 0 numbers

chowla : Int -> Int -> Int
chowla num denom =
    case remainderBy denom num of
        0 ->
            denom

        _ ->
            0

```

The Perl looks like this:

```
package Chowla;

use Moo;

sub list {
    my ($self,$max) = @_;
    my @chowla_numbers;
    for my $n (1..$max) {
        push @chowla_numbers, $self->n($n);
    }
    return \@chowla_numbers;
}

sub n {
    my ($self,$num) = @_;

    my $total = 0;
    for my $n (2..$num-1) {
        if ($num % $n == 0) {
            $total += $n;
        }
    }
    return $total;
}

1;

```

Neither are particularly long, the Elm without type signatures is 15 lines. The Perl 21 lines (though if you formatted it differently it would be about the same).

The Elm code, not having a `for` loop relies on `map` and `fold` (specifically `foldl`). I could/should try the Perl with a map. There is no equivalent of foldl in Perl. `foldl` is in interesting command, basically like a map, but collapsing in on itself via a function, in my case `+` so just summing all the numbers.

Testing the code in Perl was as ever easy, Perl's Test2:V0 is a great tool. On the Elm side I am getting more comfortable with the testing, I did not use any of the built in fuzzing on this one, I will have to explore it more.

Over the next week I have some more Go code I want to work on for [https:://fantasy-judo.com](https:://fantasy-judo.com) and I'd like to work on modernising my [https://www.vwjl.net/](https://www.vwjl.net/) a little more, the combat simulation code is where my attention should go. Given that, I may not live stream the weekly challenge this week, but might stream some general hacking about.

Tags: [perl](http://perl.kiwi/tales/tag/perl/)[elm](http://perl.kiwi/tales/tag/elm/)[perlweeklychallenge](http://perl.kiwi/tales/tag/perlweeklychallenge/)