開発環境
- 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 3(Functions)のExercises 3-1、2.を取り組んでみる。
Exercises 3-1、2.
コード(Emacs)
#!/usr/bin/env perl6 # -*- coding: utf-8 -*- say '3-1.'; sub right-justify($input-string) { say ' ' x (70 - $input-string.chars) ~ $input-string; } right-justify 'Larry Wall'; sub right-justify1($input-string) { ' ' x (70 - $input-string.chars) ~ $input-string; } say chars(right-justify1 'Larry Wall') == 70; say '3-2.'; sub do-twice($code, $value) { $code($value); $code($value); } sub greet { say 'Hello World!'; } sub print-twice($value) { say $value; say $value; } do-twice(&print-twice, "What's up doc"); sub do-four($code, $value) { do-twice($code, $value); do-twice($code, $value); } do-four(&print-twice, "What's up doc 2"); say '3-3.'; sub grid { print '+'; say ' - - - - +' x 2; do-four(&say,'| | |'); print '+'; say ' - - - - +' x 2; do-four(&say,'| | |'); print '+'; say ' - - - - +' x 2; } grid; sub grid4 { print '+'; say ' - - - - +' x 4; do-four(&say,'| | | | |'); print '+'; say ' - - - - +' x 4; do-four(&say,'| | | | |'); print '+'; say ' - - - - +' x 4; do-four(&say,'| | | | |'); print '+'; say ' - - - - +' x 4; do-four(&say,'| | | | |'); print '+'; say ' - - - - +' x 4; } grid4;
入出力結果(Terminal, REPL)
$ ./sample1.pl 3-1. Larry Wall True 3-2. What's up doc What's up doc What's up doc What's up doc What's up doc 2 What's up doc 2 What's up doc 2 What's up doc 2 What's up doc 2 What's up doc 2 What's up doc 2 What's up doc 2 3-3. + - - - - + - - - - + | | | | | | | | | | | | + - - - - + - - - - + | | | | | | | | | | | | + - - - - + - - - - + + - - - - + - - - - + - - - - + - - - - + | | | | | | | | | | | | | | | | | | | | + - - - - + - - - - + - - - - + - - - - + | | | | | | | | | | | | | | | | | | | | + - - - - + - - - - + - - - - + - - - - + | | | | | | | | | | | | | | | | | | | | + - - - - + - - - - + - - - - + - - - - + | | | | | | | | | | | | | | | | | | | | + - - - - + - - - - + - - - - + - - - - + $
0 コメント:
コメントを投稿