2017年5月22日月曜日

開発環境

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)の Exercise: The given … when switch statement.を取り組んでみる。

Exercise: The given … when switch statement.

コード(Emacs)

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

for (7, 42, 43, 100) -> $value {
    say $value;
    given $value {
        when 0..9 { say 'One digit' }
        when 10..99 { say 'Two digits'; proceed}
        when 42 { say 'The response to the ultimate question' }
        when /^\d**3$/ { say 'Three digits' }
        default { say 'More than three digits' }
    }
}

say 'ANSWER';
for (7, 42, 43, 100) -> $value {
    say $value;
    given $value {
        when 0..9 { say 'One digit' }
        when 10..99 {
            say 'Two digits';
            say 'The response to the ultimate question' if $value == 42;
        }
        when /^\d**3$/ { say 'Three digits' }
        default { say 'More than three digits' }
    }
}

入出力結果(Terminal, REPL)

$ ./sample01.pl
7
One digit
42
Two digits
The response to the ultimate question
43
Two digits
More than three digits
100
Three digits
ANSWER
7
One digit
42
Two digits
The response to the ultimate question
43
Two digits
100
Three digits
$

0 コメント:

コメントを投稿

Comments on Google+: