SourceSecond » 履歴 » バージョン 2
Yuumi Yoshida, 2008-05-23 10:10
| 1 | 1 | Yuumi Yoshida | = 第2回目のソースコード = |
|---|---|---|---|
| 2 | |||
| 3 | リスト1:ジャンケン(Javaの場合) |
||
| 4 | {{{ |
||
| 5 | // Java |
||
| 6 | import static java.lang.Math.*; |
||
| 7 | public class Jyanken { |
||
| 8 | |||
| 9 | public static String guu_choki_pa(double r) { |
||
| 10 | if (r < 0.33) { |
||
| 11 | return "Guu"; |
||
| 12 | } else if (r < 0.66) { |
||
| 13 | return "Choki"; |
||
| 14 | } else { |
||
| 15 | return "Pa"; |
||
| 16 | } |
||
| 17 | } |
||
| 18 | |||
| 19 | public static void jyanken() { |
||
| 20 | System.out.println(guu_choki_pa(random())); |
||
| 21 | } |
||
| 22 | |||
| 23 | public static void main(String args[]) { |
||
| 24 | for (int i = 0; i < 10; i++) { |
||
| 25 | jyanken(); |
||
| 26 | } |
||
| 27 | } |
||
| 28 | } |
||
| 29 | }}} |
||
| 30 | |||
| 31 | リスト2:ジャンケン(Gaucheの場合) |
||
| 32 | {{{ |
||
| 33 | (use srfi-27) |
||
| 34 | |||
| 35 | (define (guu-choki-pa r) |
||
| 36 | (cond ((< r 0.33) "Guu") |
||
| 37 | ((< r 0.66) "Choki") |
||
| 38 | (else "Pa"))) |
||
| 39 | |||
| 40 | (define (jyanken) |
||
| 41 | (print (guu-choki-pa (random-real)))) |
||
| 42 | |||
| 43 | (dotimes (i 10) (jyanken)) |
||
| 44 | }}} |
||
| 45 | |||
| 46 | リスト5:S式を与えると対応するHTMLを出力するプログラム |
||
| 47 | {{{ |
||
| 48 | (define (print-html e) |
||
| 49 | (cond ((list? e) |
||
| 50 | (print-open-tag (car e)) |
||
| 51 | (print-html-list (cdr e)) |
||
| 52 | (print-close-tag (car e))) |
||
| 53 | (else (display e)))) |
||
| 54 | |||
| 55 | (define (print-html-list s) |
||
| 56 | (cond ((null? s) #f) |
||
| 57 | (else |
||
| 58 | (print-html (car s)) |
||
| 59 | (print-html-list (cdr s))))) |
||
| 60 | |||
| 61 | (define (print-open-tag tag) |
||
| 62 | (display "<") |
||
| 63 | (display tag) |
||
| 64 | (display ">")) |
||
| 65 | |||
| 66 | (define (print-close-tag tag) |
||
| 67 | (display "</") |
||
| 68 | (display tag) |
||
| 69 | (display ">")) |
||
| 70 | |||
| 71 | (define gauche-page |
||
| 72 | '(html |
||
| 73 | (head (title "Gauche Web")) |
||
| 74 | (body (h1 "Gauche Web Page") |
||
| 75 | (table (tr (td 1)(td "lisp")) |
||
| 76 | (tr (td 2)(td "scheme")))))) |
||
| 77 | |||
| 78 | (print-html gauche-page) |
||
| 79 | }}} |