2011-03-30 52 views
0

我想解決如何通過提示用戶名稱然後從函數中獲取該列表來添加名稱到Prolog中的列表,但我不知道如何追加工程在這種情況下:添加變量到列表中的序言

test:- 
    addname(Names), 
    write(Names). 

addname(Names):- 
    write('Enter name followed by . (s. to stop)'), 
    read(Name), nl, 
    (Name = 's') -> 
     true; %% exit procedure 
     append(Names, Name, New), 
     addname(Names). 

有人可以指向正確的方向嗎?

回答

1

也許是這樣的:

get_names(Names) :- 
    writeln('Enter name followed by . (s. to stop)'), 
    read(Name), 
    add_name(Name, Names). 

add_name(s, []) :- !. 

add_name(Name, [Name | Names]) :- 
    get_names(Names). 

用法:

?- get_names(Names). 
Enter name followed by . (s. to stop) 
|: 'John'. 
Enter name followed by . (s. to stop) 
|: 'Mary'. 
Enter name followed by . (s. to stop) 
|: s. 
Names = ['John', 'Mary'].