我需要一些帮助,我是Prolog的新手,我需要显示一个我初始化的变量,但是我一直都会遇到错误,任何人都可以帮助我吗?这是代码:
:- use_module(library(lists), [member/2]).
:- use_module(contestlib, [for/3]).
knights(N,Max) :-
PlacedKnights = [],
findall((X,Y),(for(X,1,N),for(Y,1,N)),Squares),
knights(Squares,PlacedKnights,Max).
knights(N,Max,PlacedKnights) :-
findall((X,Y),(for(X,1,N),for(Y,1,N)),Squares),
knights(Squares,PlacedKnights,Max).
knights([],PlacedKnights,Max) :-
# length(PlacedKnights,Max).
knights([(X,Y)|Squares],PlacedKnights,Max) :-
( is_attacked(X,Y,PlacedKnights) ->
knights(Squares,PlacedKnights,Max)
;
knights(Squares,[knight(X,Y)|PlacedKnights],Max1),
knights(Squares,PlacedKnights,Max2),
Max is max(Max1,Max2)
).
is_attacked(X,Y,PlacedKnights) :-
( NX is X - 1, NY is Y - 2
; NX is X - 1, NY is Y + 2
; NX is X + 1, NY is Y - 2
; NX is X + 1, NY is Y + 2
; NX is X - 2, NY is Y - 1
; NX is X - 2, NY is Y + 1
; NX is X + 2, NY is Y - 1
; NX is X + 2, NY is Y + 1
),
member(knight(NX,NY),PlacedKnights).
如果完成了knights子句,我需要在结尾处显示PlacedKnights 我试过这个,但我得到错误:
错误:=字符'预期,发现1'
knights(N,Max,Display) :-
PlacedKnights = [],
findall((X,Y),(for(X,1,N),for(Y,1,N)),Squares),
knights(Squares,PlacedKnights,Max), Display = PlacedKnights.