開発環境
- 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 7(Strings)の Exercises 7-1.を取り組んでみる。
Exercises 7-1.
コード(Emacs)
#!/usr/bin/env perl6 # -*- coding: utf-8 -*- sub counter1(Str $word, Str $letter) { my $count = 0; my $i = 0; while (True) { my $j = $word.index($letter, $i); if ($j) { $count++; $i = $j + 1; } else { last; } } $count; } say counter1('banana', 'a'); sub counter2(Str $word, Str $letter) { my $count = 0; for 0..$word.chars -> $i { $count++ if $word.substr($i, 1) eq $letter; } $count; } say counter2('banana', 'a');
入出力結果(Terminal, REPL)
$ ./sample1.pl 3 3 $
0 コメント:
コメントを投稿