%% 知識情報処理実習 r8の練習問題 解答例 % 1. attendants([],[]). attendants([(Name,Age)|L],[Name|S]) :- attendants(L,S). test8_1(NL) :- attendants( [ (ann,80),(bob,40),(jim,20),(liz,16),(tom,65) ], NL ). %% テストケースは問題文から copy&paste してプログラム中に記述しておくことで、 %% 実行時の打ち間違いを防げる。 % 2. average_age(L,Y) :- ages_list(L,YL), average(YL,Y). ages_list([],[]). ages_list([(Name,Age)|L],[Age|Ys]) :- ages_list(L,Ys). sum_list([],0). sum_list([X|Y],N) :- sum_list(Y,N1), N is N1+X. list_length([],0). list_length([_|Y],N) :- list_length(Y,N1), N is N1+1. average(L,V) :- sum_list(L,S), list_length(L,N), V is S/N. test8_2(Y) :- average_age( [ (ann,80),(bob,40),(jim,20),(liz,16),(tom,65) ], Y ).