開発環境
- OS X El Capitan - Apple (OS)
- Emacs (Text Editor)
- Prolog (プログラミング言語)
- GProlog(GNU Prolog)(処理系)
7つの言語 7つの世界 (Bruce A. Tate (著)、まつもとゆきひろ (監訳)、田和 勝 (翻訳)、オーム社)の第4章(Prolog)、4.3(2日目: Wapner判事まで15分)、セルフスタディ2日目.(リスト、逆順、最小、ソート)を取り組んでみる。
セルフスタディ2日目.(リスト、逆順、最小、ソート)
コード(Emacs)
my_reverse([], []). my_reverse([Head|Tail], List) :- my_reverse(Tail, List1), append(List1, [Head|[]], List). my_min([Head|[]], Head). my_min([Head|Tail], Min) :- my_min(Tail, Min), Min =< Head. my_min([Head|Tail], Head) :- my_min(Tail, Min), Head < Min. my_sort([], []). my_sort(List1, [Min|Tail]) :- append(A, [Min|B], List1), my_min(List1, Min), append(A, B, List2), my_sort(List2, Tail).
入出力結果(Terminal)
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 Copyright (C) 1999-2013 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
0 コメント:
コメントを投稿