2017年5月21日日曜日

開発環境

Think Perl 6: How to Think Like a Computer Scientist (Laurent Rosenfeld(著)、Allen B. Downey(著)、Oreilly & Associates Inc)のPart 1(Starting with the basics)、Chapter 10(Hashes)の Exercise: 10-5.を取り組んでみる。

Exercise: 10-5.

コード(Emacs)

#!/usr/bin/env perl6
# -*- coding: utf-8 -*-

say '5.';

my %cmudict;

for 'cmudict-0.7b.txt'.IO.lines {
    my ($k, $v) = $_.split('  ');
    %cmudict{lc $k} = $v;
}

my %words = map {$_ => 1}, 'words.txt'.IO.lines;

for %words.keys {
    my $cmu = %cmudict{$_};
    if $cmu {
        my $word1 = $_.substr(1);
        if %words{$word1}:exists {
            my $cmu1 = %cmudict{$word1};
            if $cmu1 and $cmu1 eq $cmu {
                my $word2 = $_.substr(0, 1) ~ $_.substr(2);
                if %words{$word2}:exists {
                    my $cmu2 = %cmudict{$word2};
                    if $cmu2 and $cmu2 eq $cmu {
                        say "$_, $word1, $word2";
                    }
                }
            }
        }
    }
}

入出力結果(Terminal, REPL)

$ ./sample5.pl
5.
aah, ah, ah
llamas, lamas, lamas
scent, cent, sent
llama, lama, lama
$

0 コメント:

コメントを投稿