From a7820f2890db44f309166163a93a8fe53ed413ef Mon Sep 17 00:00:00 2001 From: Ttsoccer Date: Wed, 20 Jan 2016 01:25:43 +0900 Subject: [PATCH] 20151125kadai --- 20151125/src/enshu_20151125/append.swi | 3 +++ 20151125/src/enshu_20151125/eight-queen.swi | 12 ++++++++++++ 20151125/src/enshu_20151125/hanoi.swi | 5 +++++ 20151125/src/enshu_20151125/like.swi | 4 ++++ 4 files changed, 24 insertions(+) create mode 100644 20151125/src/enshu_20151125/eight-queen.swi create mode 100644 20151125/src/enshu_20151125/hanoi.swi diff --git a/20151125/src/enshu_20151125/append.swi b/20151125/src/enshu_20151125/append.swi index 37d9e4e5..f9dd6aec 100755 --- a/20151125/src/enshu_20151125/append.swi +++ b/20151125/src/enshu_20151125/append.swi @@ -1,5 +1,8 @@ #!/usr/bin/env prolog +my_append([],Xs,Xs). +my_append([X | Ls],Ys,[X | Zs]) :- my_append(Ls,Ys,Zs). + % check with % prolog -f ./append.swi -t 'my_append("[a,b,c]", "[d,e,f]", X).' diff --git a/20151125/src/enshu_20151125/eight-queen.swi b/20151125/src/enshu_20151125/eight-queen.swi new file mode 100644 index 00000000..716bc607 --- /dev/null +++ b/20151125/src/enshu_20151125/eight-queen.swi @@ -0,0 +1,12 @@ +queen(Q) :- perm([1,2,3,4,5,6,7,8],Q),safe(Q). + +perm([],[]). +perm(Xs,[Z|Zs]) :- select(Z,Xs,Ys),perm(Ys,Zs). + +safe([Qt|Qr]) :- not(attack(Qt,Qr)),safe(Qr). +safe([]). + +attack(X,Xs) :- attack_sub(X,1,Xs). +attack_sub(X,N,[Y|Ys]) :- (X=:= Y+N ; X=:= Y-N). +attack_sub(X,N,[Y|Ys]) :- N1 is N+1,attack_sub(X,N1,Ys). + diff --git a/20151125/src/enshu_20151125/hanoi.swi b/20151125/src/enshu_20151125/hanoi.swi new file mode 100644 index 00000000..8618fd33 --- /dev/null +++ b/20151125/src/enshu_20151125/hanoi.swi @@ -0,0 +1,5 @@ +hanoi(1,From,To,_) :- write([From,to,To]), nl. +hanoi(N,From,To,Via) :- + N1 is N-1, hanoi(N1,From,Via,To), + write([From,to,To]),nl, + hanoi(N1,Via,To,From). \ No newline at end of file diff --git a/20151125/src/enshu_20151125/like.swi b/20151125/src/enshu_20151125/like.swi index e47474f2..b8eb9662 100755 --- a/20151125/src/enshu_20151125/like.swi +++ b/20151125/src/enshu_20151125/like.swi @@ -1,5 +1,9 @@ #!/usr/bin/env prolog +like(taro,coffee). +like(hanako,tea). +like(taro,cocoa). + % check with % prolog -f ./like.swi -t 'like(X,Y).'