prolog style



Hello,

I am fairly new to Prolog programming, and I would like to have some
comment on the design and style of this somewhat larger Prolog
program.

It is an enumerator for the solutions to the Connect 4 problem. I'd
like to use both arguments to the play predicate for input and output.

I have following questions:

1) Is this a sound design to tackle this problem?
2) Do you see performance issues?
3) Do you see simplifications?
4) I read some discussions about "Pure" and "Impure" Prolog
programming style. Would you call this "Pure"?

Thanks!

Bart.


Sorry for the somewhat lengthy code snippet, but it is relevant to my
question:


%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

board([[-,-,-,-,-,-],[-,-,-,-,-,-],[-,-,-,-,-,-],[-,-,-,-,-,-],
[-,-,-,-,-,-],[-,-,-,-,-,-],[-,-,-,-,-,-]]).

cols2rows([[HA|A],[HB|B],[HC|C],[HD|D],[HE|E],[HF|F],[HG|G]],[Row|
Rows]):-
atoms2list(HA,HB,HC,HD,HE,HF,HG,Row),
cols2rows([A,B,C,D,E,F,G],Rows).

cols2rows([[],[],[],[],[],[],[]],[]).

atoms2list(A,B,C,D,E,F,G,[A,B,C,D,E,F,G|[]]).

peek([], +).

peek([H|List],H).

% insertcol(Player,[],[]).

insertcol(Player,[H|Col],[H|Col]):-
not(H = -),!,
fail.

insertcol(Player,[H|Col],[Player|Col]):-
peek(Col, Next),
not(Next = -).

insertcol(Player,[H|Col],[H|Col1]):-
insertcol(Player, Col, Col1).

addhead2list([],Target,Target).

addhead2list([H|Source],Target,[H|Target]).

addallheads2list([],Target,Target).

addallheads2list([H|Lists],Target,NewTarget):-
addhead2list(H,Target,IntTarget),
addallheads2list(Lists,IntTarget,NewTarget).

deletehead([],[]).
deletehead([_|Tail],Tail).


deleteheads([],[]).
deleteheads([First|Tail],[First1|NewList]):-
deletehead(First, First1),
deleteheads(Tail,NewList).

removenulls([],[]).
removenulls([[]|Tail],Tail1):-
removenulls(Tail,Tail1).
removenulls([A|Tail],[A|Tail1]):-
removenulls(Tail,Tail1).



diag([],[[]],[[]]).
diag([],[],[]).
diag([],[[]],[]).
diag([],[],[[]]).

diag([Diag|Result], Process, []):-
addallheads2list(Process,[],Diag),
deleteheads(Process,Process2),
removenulls(Process2,Process3),
diag(Result,Process3,[]).

diag([Diag|Result], Process, Waiting):-
addhead2list(Waiting,Process,Process1),
deletehead(Waiting,Waiting1),
addallheads2list(Process1,[],Diag),
deleteheads(Process1,Process2),
removenulls(Process2,Process3),
diag(Result,Process3,Waiting1),!.


check([A,A,A,A],A).
check([_,A,A,A,A],A).
check([A,A,A,A,_],A).
check([A,A,A,A,_,_],A).
check([_,A,A,A,A,_],A).
check([_,_,A,A,A,A],A).
check([A,A,A,A,_,_,_],A).
check([_,A,A,A,A,_,_],A).
check([_,_,A,A,A,A,_],A).
check([_,_,_,A,A,A,A],A).

perform(Seq,A):-
check(Seq,A),
not(A = -).

doColumns([First|Board],Winner):-
perform(First,Winner).

doColumns([First|Board],Winner):-
doColumns(Board,Winner).

doRows(Board,Winner):-
cols2rows(Board,Board1),
doColumns(Board1,Winner).

doDiags(Board,Winner):-
diag(Board1,[],Board),
doColumns(Board1,Winner).

doOtherDiags(Board,Winner):-
cols2rows(Board,Board1),
diag(Board2,[],Board1),
doColumns(Board2,Winner).

win(Board,Winner):-
doColumns(Board,Winner).

win(Board,Winner):-
doRows(Board,Winner).

win(Board,Winner):-
doDiags(Board,Winner).

win(Board,Winner):-
doOtherDiags(Board,Winner).


doit(_,Board, Winner, []):-
win(Board,Winner).

doit(Player, Board, Winner,[Col|NewList]):-
move(Player,Board,Board1,Col),
player(Player,Player1),
doit(Player1,Board1,Winner,NewList).

move(Player,[Col|Rest],[NewCol|Rest],1):-
insertcol(Player,Col,NewCol).

move(Player,[Col|Rest],[Col|Rest1],Id1):-
move(Player,Rest,Rest1,Id),
Id1 is Id + 1.

play(Steps, Winner):-
board(Start),
doit( +, Start, Winner, Steps).

player(o,+).
player(+,o).

%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%










.



Relevant Pages

  • Re: mfc pitfalls
    ... In most MFC apps, you are writing code in that kind ... virtual methods usually work better than callbacks for most ... no syntax in the design for function pointers. ... programming in OO environment requires new ...
    (microsoft.public.vc.mfc)
  • Re: mfc pitfalls
    ... I see no callbacks in MFC. ... but I see no callbacks. ... premises of one of them as a basis of design or implementation in one of the others. ... programming in OO environment requires new ...
    (microsoft.public.vc.mfc)
  • Re: SENIOR A.I. SOFTWARE ENGINEER POSITION - $90-120K - Dallas
    ... May design and build prototype applications. ... Expert C and C++ programming experience. ... Java / J2EE Architecture ... Understands standard software modeling techniques ...
    (comp.ai.philosophy)
  • Re: mfc pitfalls
    ... There are design criteria, ... Data flow (flowcharts) is one of the UML diagrams ... programming in OO environment requires new ...
    (microsoft.public.vc.mfc)
  • The most popular A- Z, CAx, CAD, CAM, CAE, electronics, EDA, LSI, PCB, FPGA, VHDL, & Other D
    ... Nauticus Early Design ... programming of sheet metal cutting and punching machines. ... Lantek Expert II.1 Lantek Expert Cut Oxifuel/Plasma ... Rockwell Allen Bradley Programming Software, RSLogix 5, RSLogix 500, ...
    (microsoft.public.windowsxp.basics)