如何让两个驱动器谓词在prolog中工作?

时间:2012-11-18 14:09:42

标签: prolog win-prolog

begin:- 

go,
initialise.             % drive predicate

begin:-
write('Sorry cannot help'),nl,nl.



go:-
write('Below is the list of the current toys available within the 

store.'),nl,nl,nl,nl,
loop_through_list([car, football, action_men, train_tracks, lego, football, bikes, control_car, drees_up, doll, bear, furbies, craft, doll_house]).
loop_through_list([Head|Tail]) :-
    write(Head),
    write(' '),
    loop_through_list(Tail).
initialise:-
nl,nl,nl,nl,
tab(40),write('******************************************'),nl,
tab(40),write('*** TOY GENERATING SYSTEM ***'),nl,
tab(40),write('******************************************'),nl,nl,
write('Please answer the following questions'),
write(' y (yes) or n (no).'),nl,nl, nl, nl.

这里的问题是go:-initialise:-在单独使用时会分开工作,但在放在一起时却不能。所有nl s都存在问题吗?

1 个答案:

答案 0 :(得分:2)

问题是go/0无法正常工作。当它打印列表时,它最后会失败,这意味着执行将在之后停止。因此,当您运行begin/0时,initialize/0将永远无法运行。

要解决此问题,您需要为loop_through_list/0添加基本案例:

loop_through_list([]).
loop_through_list([Head|Tail]) :-
    write(Head),
    write(' '),
    loop_through_list(Tail).

作为旁注,“print_list”将是loop_through_list/0

的更具描述性的名称