2015年7月17日金曜日

開発環境

Schemeの処理系(解釈系、評価器、レジスタ計算機を翻訳した命令列中心のより、もう少しC言語の特性を使った書き方をしたもの(label, gotoではなく、関数を呼び出すとか))を少しずつ書き進めてめていくことに。

Land of Lisp(じゃなくてScheme)で必要になった、乱数を生成する、random 手続きを実装。(gmpを利用。)

参考書籍等

kscheme

コード(BBEdit, Emacs)

number_z.c

#include "number_z.h"
#include <gmp.h>
#include "stopif.h"

data_s number_z_new(char *in) {
  data_s out = {.type = Z};
  Stopif(mpz_init_set_str(out.data.z, in, 10) == -1, exit(1),
         "整数割り当て失敗");
  return out;
}

data_s number_z_copy(data_s in) {
  data_s out = {.type = Z};
  mpz_init_set(out.data.z, in.data.z);
  return out;
}

void number_z_free(data_s in) { mpz_clear(in.data.z); }

void number_z_print(FILE *stream, data_s in) {
  mpz_out_str(stream, 10, in.data.z);
}

bool number_z_eq(data_s in1, data_s in2) {
  return mpz_cmp(in1.data.z, in2.data.z) == 0;
}

data_s number_z_quotient(data_s in1, data_s in2) {
  data_s out = {.type=Z};
  mpz_init(out.data.z);
  mpz_tdiv_q(out.data.z, in1.data.z, in2.data.z);
  return out;
}

#include "list_operations.h"
data_s prim_number_z_z2r(data_s in) {
  data_s out = {.type=R};
  mpf_init(out.data.r);
  mpf_set_z(out.data.r, car(in).data.z);
  return out;
}

data_s number_z_random(data_s in) {
  data_s out = {.type=Z};
  mpz_init(out.data.z);      
  mpz_urandomm(out.data.z, state, in.data.z);
  return out;
}

sample.scm

(begin  
  (define for-each
    (lambda (proc items)
      (if (not (null? items))
          (begin (proc (car items))
                 (for-each proc (cdr items))))))  

  (define enumerate-interval
    (lambda (low high)
      (if (> low high)
          '()
          (cons low
                (enumerate-interval (+ low 1) high)))))

  (for-each (lambda (n)
              (for-each (lambda (m)
                          (display (random n))
                          (display " "))
                        (enumerate-interval 1 10))
              (newline))
            (enumerate-interval 1 100))
  (quote done)))

入出力結果(Terminal(kscm), REPL(Read, Eval, Print, Loop))

$ kscheme sample.scm
0 0 0 0 0 0 0 0 0 0 
0 1 1 1 0 1 1 1 1 1 
2 1 2 2 1 1 0 2 1 0 
3 2 1 2 2 3 2 0 0 1 
0 0 2 2 4 3 2 4 3 2 
1 3 2 2 3 2 0 1 0 3 
3 6 4 3 0 3 6 5 3 3 
7 4 4 0 0 3 5 7 0 7 
1 4 7 8 5 8 2 3 2 1 
3 5 3 8 0 8 7 0 3 3 
0 6 5 9 0 8 10 2 6 3 
2 8 10 4 3 5 10 8 7 2 
6 4 8 6 1 2 12 12 8 4 
6 1 0 0 8 6 3 7 9 11 
8 3 8 6 14 5 12 8 10 0 
9 3 3 6 10 11 5 6 3 6 
15 14 0 5 16 12 8 3 8 8 
7 2 15 2 17 14 16 5 13 2 
18 4 10 11 16 17 15 14 7 0 
18 14 3 17 4 13 9 4 4 11 
10 7 19 4 17 11 12 2 10 13 
14 17 8 4 0 8 2 19 14 11 
16 0 11 11 1 8 2 10 7 1 
8 9 23 14 23 21 0 3 5 12 
17 19 1 10 15 8 20 2 7 23 
6 25 4 11 11 18 24 4 11 4 
6 13 14 22 23 16 1 9 10 4 
15 27 2 17 25 24 25 4 25 14 
0 14 8 0 20 3 0 4 4 2 
19 23 25 1 17 21 25 21 16 2 
30 28 17 21 3 12 8 11 18 5 
22 4 11 3 3 6 12 27 23 21 
32 31 10 32 18 4 11 7 27 25 
29 5 12 5 3 3 19 22 22 31 
17 19 32 28 11 6 17 3 11 1 
0 7 3 1 7 24 3 12 24 30 
20 27 2 18 19 16 11 13 3 25 
26 21 3 35 19 25 20 24 2 6 
32 32 7 28 14 1 23 16 10 0 
14 32 5 9 2 17 8 38 3 1 
39 12 1 6 10 35 35 27 11 32 
0 34 31 2 18 11 17 22 11 36 
24 23 42 23 2 35 1 26 31 8 
15 30 27 19 10 22 3 31 0 24 
10 8 32 31 19 26 4 39 38 10 
38 19 39 14 19 38 39 39 13 44 
28 38 34 18 12 4 14 24 43 2 
34 18 42 21 41 27 24 13 47 37 
42 3 19 32 31 35 48 7 37 35 
10 37 38 34 49 28 14 2 45 27 
38 15 13 36 22 46 6 39 36 11 
49 37 24 5 0 22 10 47 19 19 
30 11 38 21 22 14 11 45 35 0 
52 39 44 35 13 41 19 38 16 47 
0 7 15 27 28 32 10 25 41 0 
26 22 51 6 43 7 27 45 1 10 
3 2 28 24 51 21 41 35 6 37 
47 46 52 13 19 12 16 3 32 22 
4 21 34 52 49 55 5 11 54 31 
2 56 7 52 40 20 5 16 16 50 
30 19 14 2 37 18 19 32 42 34 
16 59 17 45 6 46 0 22 30 8 
42 41 34 23 33 11 9 28 42 58 
31 29 14 3 34 14 16 45 43 52 
43 63 55 51 45 13 42 28 59 5 
52 12 9 43 64 65 53 37 42 4 
3 26 36 65 9 43 36 18 58 47 
35 20 27 66 34 28 23 60 13 50 
21 9 9 67 7 28 3 12 26 27 
35 27 64 10 47 25 28 34 25 40 
33 39 63 42 66 15 49 13 70 46 
3 44 41 35 51 34 58 19 53 2 
54 41 64 38 25 34 16 9 31 60 
44 65 19 4 71 33 51 58 40 49 
5 48 41 42 70 62 68 58 30 42 
49 38 26 20 9 64 15 31 23 67 
64 1 52 4 24 50 59 38 11 66 
19 18 71 59 46 3 7 14 70 30 
51 49 15 41 33 21 76 69 29 71 
32 71 73 58 31 56 76 63 77 9 
32 28 43 39 75 55 18 22 56 49 
33 21 40 9 42 57 65 71 31 64 
26 74 22 57 2 77 69 66 26 60 
58 28 27 49 33 10 0 47 75 14 
81 55 6 32 32 11 7 71 30 46 
30 48 6 9 47 35 67 11 47 66 
55 47 10 36 15 78 21 38 59 81 
71 60 10 51 2 71 50 22 80 19 
54 56 14 7 27 43 2 52 66 39 
12 80 76 22 58 3 34 88 57 33 
60 14 79 11 53 18 30 14 14 80 
54 44 58 74 65 69 53 34 8 16 
61 37 10 24 73 91 27 10 25 77 
11 43 3 86 13 0 47 46 63 69 
68 83 28 86 85 92 50 51 41 76 
25 38 2 21 37 59 58 28 21 0 
18 20 67 69 47 33 40 88 43 44 
52 46 5 8 83 1 59 33 72 89 
93 19 96 42 64 92 24 20 89 25 
70 38 85 90 25 8 52 22 16 5 
done
$

0 コメント:

コメントを投稿

Comments on Google+: