0
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都有问题吗?

4

1 回答 1

2

问题是go/0它不能正常工作。当它打印列表时,它在最后失败了,这意味着执行将在之后停止。因此,当你运行时begin/0initialize/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

于 2012-11-18T14:50:46.893 回答