開発環境
- macOS Sierra - Apple (OS)
- Emacs (Text Editor)
- Perl 6 (プログラミング言語)
- Rakudo(コンパイラ、実装)
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 11(Case Study: Data Structure Selection)の Random Numbers、Exercise: 5.を取り組んでみる。
Exercise: 5.
コード(Emacs)
#!/usr/bin/env perl6
# -*- coding: utf-8 -*-
sub histogram($string) {
my %histo;
for $string.comb -> $letter {
%histo{$letter}++;
}
return %histo;
}
sub choose_from_hist(%histogram) {
my $letter = %histogram.keys.pick;
return $letter, %histogram{$letter} / [+] %histogram.values;
}
say choose_from_hist(histogram("We all live in a yellow submarine"));
for 1..10 {
say choose_from_hist(histogram("aab"));
}
入出力結果(Terminal, REPL)
$ ./sample5.pl (b 0.030303) (a 0.666667) (a 0.666667) (b 0.333333) (b 0.333333) (a 0.666667) (a 0.666667) (b 0.333333) (b 0.333333) (b 0.333333) (a 0.666667) $
0 コメント:
コメントを投稿