试图学习Prolog,无法弄清楚什么是错的

时间:2012-11-10 16:16:44

标签: turbo-prolog

举一个小例子,我想在列表中插入符号

domains
    element = symbol
    list = element*
    position = integer
predicates
    insert (element, position, list, list) %(input,input,input,output)
clauses 
    insert (E,_,[],[E]). 
    insert (E, 1,[H|T],[E|[H|T]]).       %(I insert E on the "first" position)
    % I get errors how E and T are not bound. I have no idea how to make them bound
    insert (E,P,[H|T],[H1|T1]) :-
           P > 1,
           P1 = P - 1,
           insert (E,P1,T,T1).

它不起作用......但我不知道为什么。嗯它有点工作。我希望它向我显示outputList = [NEW_LIST],而不是显示每个symbolName=_然后outputList = [_,_,_,_]

1 个答案:

答案 0 :(得分:0)

显然符号数据类型存在一些问题,如果我使用整数,它会自行修复。

此外,这是必要的:

domains
    element = integer
     list = element*
     position = integer
predicates
     insert (element, position, list, list) %(input,input,input,output)
clauses 
     insert (E,_,[],[E]). 
     insert (E, 1,[H|T],[E|[H|T]]).
     insert (E,P,[H|T],[H|T1]) :-  % has to be "H" (or anything else) in both
          P > 1,                   % so prolog understands what we are trying to do
          P1 = P - 1,              % don't really understand why though
          insert (E,P1,T,T1).      % I might be wrong