2012-11-18 76 views
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都有問題嗎?如何讓兩個驅動謂詞在prolog中工作?

+0

不應該有一個冒號'初始化'後?即開始: - 去,初始化。 –

+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

+0

謝謝這麼多,這個作品! –

+0

@W_K不用客氣;順便說一句,你也可以使用'writef/2' [http://www.swi-prolog.org/pldoc/doc_for?object=writef/2]爲io:'writef(「%w」,[Head])' –

+0

@thanosQR:格式/ 2是更常見的名稱。你看到一個理由,爲什麼有'writef/2'? – false