2011-09-21 45 views
1

有另一個問題: 有一個單詞列表。如果單詞的長度超過給定長度(例如4),它將被放入另一個列表中。Sicstus Prolog - 將單詞放入列表

我想:

require_min_length([], _, []). 
require_min_length([Word|Words], Minl, [List]):- 
    word_length(Word, W), % method word_length return the length of a word. 
    (W >= Minl -> append(Word, List, List), require_min_length(Words, Minl, List);  
    require_min_length(Words, Minl, List)). 

結果我:

| ?- Words=["ABCD", "ABCDE", "AAA"], require_min_weight(Words, 5, Lists).  
! Resource error: insufficient memory. 

正確的結果將是:

Lists = [[65, 66, 67, 68, 69]]. (% ascii) 

如何更改密碼?任何幫助請!謝謝。

回答

1

你的謂詞的問題是你使用append錯誤。

您正在使用追加(Word,List,List),這隻會在Word是空列表時成功。你也不希望將你的單詞的代碼追加到輸出列表,而是單詞本身。

考慮這樣的事情:

require_min_length([], _, []). 
require_min_length([Word|Words], Minl, NList):- 
    word_length(Word, W), % method word_length return the length of a word. 
    (W >= Minl -> NList=[Word|List] ; NList=List), 
    require_min_length(Words, Minl, List)).