2014-11-03 241 views
0

我開發了一個謂詞,它將列表List的索引Index的值替換爲Value,並創建一個新的更新列表NewList替換列表中的元素PROLOG

%replace(List,Index,Value,NewList) 

replace([_|T], 0, X, [X|T]). 
replace([H|T], I, X, [H|R]):- 
     I > -1, 
     NI is I-1, 
     replace(T, NI, X, R), !. 
replace(L, _, _, L). 

該謂詞在正常列表上工作正常,但我想使它在列表上工作,而且我有點困難。

subs([]). 
subs([Head|Tail], Index) :- 
     replace((Head), Index, 'r', Board2), 
     printRow(Board2), 
     subs(Tail). 

原版:

[ [ 0 , 1 , 2 , 3 , 4 ] , 
    [ 5 , 6 , 7 , 8 , 9 ] , 
    [ 10 , 11 , 12 , 13 , 14 ] , 
    [ 15 , 16 , 17 , 18 , 19 ] , 
    [ 20 , 21 , 22 , 23 , 23 ] 
] 

輸出:

[ [ 0 , r , 2 , 3 , 4 ] , 
    [ 5 , r , 7 , 8 , 9 ] , 
    [ 10 , r , 12 , 13 , 14 ] , 
    [ 15 , r , 17 , 18 , 19 ] , 
    [ 20 , r , 22 , 23 , 23 ] 
] 

值得注意的是,爲什麼出現這種情況,因爲它取代Index = 1每個子列表中的值。 爲了解決這個問題,我想到了實施一個計數器。由5每次迭代(每個子列表的大小),遞增索引,謂詞現在應該輸出以下(期望)名單:

所需的輸出:

[ [ 0 , r , 2 , 3 , 4 ] , 
    [ 5 , 6 , 7 , 8 , 9 ] , 
    [ 10 , 11 , 12 , 13 , 14 ] , 
    [ 15 , 16 , 17 , 18 , 19 ] , 
    [ 20 , 21 , 22 , 23 , 23 ] 
] 

而問題在於如何實施這個非常反面。該代碼應類似於以下,但有什麼我錯過了:

subs([]). 
subs([Head|Tail], Index) :- 
     replace((Head), Index, 'r', Board2), 
     printRow(Board2), 
     Index is Index + 5 
     subs(Tail, Index). 

輸出:subs(<Original List>, 7).

0 1 2 3 4 

誰能給我關於如何實現它的一些幫助嗎?

+0

我有點困惑。你想在列表清單上做些什麼?你現在有兩個'subs'子句。一個需要一個參數('subs([])。'),另一個需要兩個('subs([Head | Tail],Index):-...')。請詳細說明'L'是列表清單時'subs(L,Index)'的含義。 – lurker 2014-11-03 18:50:32

+0

它應該替換列表列表中Index ='Index'的元素。儘管它取代了董事會的每個子列表。 – Khabz 2014-11-03 19:11:40

回答

1

您的問題陳述有點不清楚。

從您的示例中,您會看到您想將列表列表視爲基本上是二維數組,並替換該數組中的單個單元格。如果是這樣,這是一個辦法(可能非最佳)這樣做:

% 
% replace a single cell in a list-of-lists 
% - the source list-of-lists is L 
% - The cell to be replaced is indicated with a row offset (X) 
% and a column offset within the row (Y) 
% - The replacement value is Z 
% - the transformed list-of-lists (result) is R 
% 
replace(L , X , Y , Z , R) :- 
    append(RowPfx,[Row|RowSfx],L),  % decompose the list-of-lists into a prefix, a list and a suffix 
    length(RowPfx,X) ,     % check the prefix length: do we have the desired list? 
    append(ColPfx,[_|ColSfx],Row) , % decompose that row into a prefix, a column and a suffix 
    length(ColPfx,Y) ,     % check the prefix length: do we have the desired column? 
    append(ColPfx,[Z|ColSfx],RowNew) , % if so, replace the column with its new value 
    append(RowPfx,[RowNew|RowSfx],R) % and assemble the transformed list-of-lists 
    . 

另一種方式(可能更優):

replace([L|Ls] , 0 , Y , Z , [R|Ls]) :- % once we find the desired row, 
    replace_column(L,Y,Z,R)     % - we replace specified column, and we're done. 
    .          % 
replace([L|Ls] , X , Y , Z , [L|Rs]) :- % if we haven't found the desired row yet 
    X > 0 ,         % - and the row offset is positive, 
    X1 is X-1 ,        % - we decrement the row offset 
    replace(Ls , X1 , Y , Z , Rs)   % - and recurse down 
    .          % 

replace_column([_|Cs] , 0 , Z , [Z|Cs]) . % once we find the specified offset, just make the substitution and finish up. 
replace_column([C|Cs] , Y , Z , [C|Rs]) :- % otherwise, 
    Y > 0 ,         % - assuming that the column offset is positive, 
    Y1 is Y-1 ,        % - we decrement it 
    replace_column(Cs , Y1 , Z , Rs)   % - and recurse down. 
    .           % 
+0

對於在我的聲明中可能存在的誤導,我很抱歉,但它已經差不多8個小時的硬核Prolog。是的,這是我正在尋找的。 我構建的謂詞(替換)適用於列表,但不適用於2d。對於subs我試圖分別分析每個子列表,並在每次迭代之後遞增索引,以便它不會回到0.我是否太離譜了? 後來我會編輯我的問題,讓更多的人可以理解我想說的話。 – Khabz 2014-11-03 20:33:07