## 2016年5月4日水曜日

### Prolog - リスト、逆順、最小、ソート

７つの言語 ７つの世界 (Bruce A. Tate (著)、まつもとゆきひろ (監訳)、田和 勝 (翻訳)、オーム社)の第4章(Prolog)、4.3(2日目: Wapner判事まで15分)、セルフスタディ2日目.(リスト、逆順、最小、ソート)を取り組んでみる。

セルフスタディ2日目.(リスト、逆順、最小、ソート)

コード(Emacs)

```my_reverse([], []).
my_reverse(Tail, List1),

my_sort([], []).
my_sort(List1, [Min|Tail]) :-
append(A, [Min|B], List1),
my_min(List1, Min),
append(A, B, List2),
my_sort(List2, Tail).

```

```GNU Prolog 1.4.4 (64 bits)
Compiled Feb  5 2016, 06:20:25 with /opt/local/bin/gcc-apple-4.2
By Daniel Diaz
| ?- ['sample2.pl'].
compiling /sample2.pl for byte code...
.../sample2.pl compiled, 16 lines read - 2230 bytes written, 17 ms

(3 ms) yes
| ?- my_reverse([], What).

What = []

yes
| ?- my_reverse([1], What).

What = [1]

yes
| ?- my_reverse([1, 2], What).

What = [2,1]

yes
| ?- my_reverse([1, 2, 3, 4, 5], What).

What = [5,4,3,2,1]

yes
| ?- my_min([1], What).

What = 1 ? a

no
| ?- my_min([1, 1], What).

What = 1 ? a

no
| ?- my_min([1, 2], What).

What = 1 ? a

no
| ?- my_min([2, 1], What).

What = 1 ? a

(1 ms) no
| ?- my_min([5, 1, 4, 2, 3], What).

What = 1 ? a

no
| ?- my_min([5, 1, 4, 2, 3, 5, 1, 4, 2, 3], What).

What = 1 ? a

(2 ms) no
| ?- my_sort([], What).

What = [] ? a

(1 ms) no
| ?- my_sort([1], What).

What = [1] ? a

no
| ?- my_sort([1, 1], What).

What = [1,1] ? a

What = [1,1]

(1 ms) no
| ?- my_sort([1, 2], What).

What = [1,2] ? a

no
| ?- my_sort([2, 1], What).

What = [1,2] ? a

(1 ms) no
| ?- my_sort([5, 1, 4, 2, 3], What).

What = [1,2,3,4,5] ? a

no
| ?- my_sort([5, 1, 4, 2, 3, 5, 1, 4, 2, 3], What).

What = [1,1,2,2,3,3,4,4,5,5] ? a

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

(17 ms) no
| ?- my_sort([5, 5, 1, 1, 4, 4, 2, 2, 3, 3], What).

What = [1,1,2,2,3,3,4,4,5,5] ? a

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

What = [1,1,2,2,3,3,4,4,5,5]

(16 ms) no
| ?-

Process prolog finished
```