2011-12-13 67 views
-1

我尝试构建将附加随机数的子列表结果列表的谓词。将元素附加到列表中的序言递归

my_predicate([], AnotherList, []). 
my_predicate([Head|List], AnotherList, Result):- 
    random(0,5,N), 
    nested_predicate(N, Head, AnotherList, SM), 
    my_predicate(List, AnotherList, Result), 
    append(SM, Result, SM2), 
    write(SM2). 

一切几乎没问题,但我无法以任何方式将SM2分配给结果。我做错了什么?

+0

你也应该张贴nested_predicate的定义,或很难尝试东西.. –

回答

2

在Prolog中,您不能为变量“赋值”。此外,在您的代码中,Result将始终绑定到空列表。

我假设你想要的是这样的:

my_predicate([], AnotherList, []). 
my_predicate([Head|List], AnotherList, Result):- 
    random(0,5,N), 
    nested_predicate(N, Head, AnotherList, SM), 
    my_predicate(List, AnotherList, SM2), 
    append(SM, SM2, Result), 
    write(Result).