English 中文(简体)
Prolog编程 - 关于下一步任务的方案是什么? [结束]
原标题:Prolog programming - what is the program regarding the next tasks? [closed]
  • 时间:2012-05-26 19:53:16
  •  标签:
  • prolog
  • logic

能帮我处理下三个Prolog方案吗?

  1. Summary of the elements in a list, and check that is divided or not divided with 3? For example, the list is [1, 2, 3] --> and the sum of the element is divided with 3, because 1+2+3=6, and 6/3=2 --> so the output should be true.

  2. 如果7是列表中的,则加倍。 例如:输入列表 - & gt; [1, 7, 3, 7, 7,7], 产出应该是 [1, 7, 7, 7, 7,7,7] 。

  3. 如果7是列表中的7, 请更改为 2, 7 2. 例如: 输入列表 - & gt; [1, 7, 2, 1], 输出应该是 [1, 2, 7, 7, 2, 2, 1]

程序是什么? 如何用 SWI -Prolog 测试?

感谢大家的期待!

问题回答

我会给你几个提示:

  1. 您需要 a) 来计算总和, b) 检查它是否除以 3 。 如果您使用 SWI- Prolog, 图书馆 < code> lists 中有一个前提 < code> lists (a), 而 < code >... 是... mod... 构造来解答 b) 。 如果您需要使用递归而不是内置的前提来计算总和 :

    Sum([XX], Acc, Sum) : - Acc1 是 Acc + X, Sum(X), Acc1, Sum。

    sum([, Acc, Acc)。

    和(列表、总和) :- 和(列表、0、总和)。

  2. 和 3. 这些是递转程序。您应该绕过列表,如果遇到7个问题,您应该替换为7个问题2,7个问题2,2个问题3,7个问题2,7个问题2。

traverse_list([],[]).
traverse_list([7|Xs], [7,7|Ps]) :-
   !,
   traverse_list(Xs,Ps).
traverse_list([X|Xs], [X|Ps]) :-
   traverse_list(Xs,Ps).

考虑修改此碎片为 3 。

我建议你考虑一下 自己去弄清楚

我假设你的老师想看你做循环 而不是使用内置的合成列表的前提

sum([FirstNum | Rest], Sum) :- sum(Rest, Sum1), Sum is FirstNum + Sum1.

然后使用“ mod” 运算符来检查可变性。 记住, 请在递归到达空集时包含一个基本大小写。 如果您能理解这一点, 其余的应该很容易。 祝你好运 。





相关问题
Prolog difference routine

I need some help with a routine that I am trying to create. I need to make a routine that will look something like this: difference([(a,b),(a,c),(b,c),(d,e)],[(a,_)],X). X = [(b,c),(d,e)]. I really ...

Python: Analyzing complex statements during execution

I am wondering if there is any way to get some meta information about the interpretation of a python statement during execution. Let s assume this is a complex statement of some single statements ...

Python nested lists and recursion problem

I m trying to process a first order logic formula represented as nested lists and strings in python so that that its in disjunctive normal form, i.e [ & , [ | , a , b ], [ | , c , d ]] ...

prolog - why this strange trace

here is the prolog code (which i sort of follow). len([],0). len([_|T],N) :- len(T,X), N is X+1. and here is the trace for it (im running linux, swi) [trace] ?- len([d,f,w,c],X). Call: (7) ...

prolog cut off in method

I have a question I would like to ask you something about a code snippet: insert_pq(State, [], [State]) :- !. insert_pq(State, [H|Tail], [State, H|Tail]) :- precedes(State, H). insert_pq(State, [...

Pragmatically adding give-aways/freebies to an online store

Our business currently has an online store and recently we ve been offering free specials to our customers. Right now, we simply display the special and give the buyer a notice stating we will add the ...

how to create a parser for search queries

for example i d need to create something like google search query parser to parse such expressions as: flying hiking or swiming -"**walking in boots **" **author:**hamish **author:**reid or ...

Applications of Unification?

What are the (practical) applications of Unification? Where it is actually being used in real world? I couldn t understand the whole idea of what it is really about and why it s considered as a part ...