你能帮我写咖啡菜单吗?
这是我尝试过的。
go :- hypothesize(Coffee),
write('Your order is : '),
write(Coffee),
write('and the price for your order = : ')
nl,
undo.
/* hypotheses to be tested */
hypothesize(moca) :- moca, !.
hypothesize(hotChocolate) :- hotChocolate, !.
hypot
我想要创建一个函数multiples(X, N, R),其中R是一个包含从X到X * N的所有X倍数的列表。
一个例子是:multiples(3, 4, [12, 9, 6, 3]),它应该给出真。
到目前为止我的代码是:
multiples(X, N, R) :- X >= 1, N >= 1, Z is X*N, contains(Z, R).
contains(Z, [Z|_]).
contains(Z, [W|V]) :- contains(Z,V), L is Z-X, L >= X, contains(L, V).
multiples(3,4,X).控制台的输出
我对prolog还不熟悉,我就是搞不懂。
我试图构建一个简单的程序,该程序接收谓词列表,搜索列表中的特定谓词,并将函数应用于该谓词的参数。
类似于这样的东西:
?- program([pred1(a,b,p), pred2(d,b,p), pred2 (a,c,p)]).
program (list1) :-
search(pred2(X,Y,p),list1).
doSomething (X,Y) % with the X and Y returned from search function, both of them.
基本上,我希望使用从目标search(pred2(X,Y,p)
我试图创建一个列表,返回数素数直到N个数字。
我有这样的代码,它指示数字是否为素数:
is_prime(2).
is_prime(3).
is_prime(P) :- integer(P), P > 3, P mod 2 =\= 0, \+has_factor(P,3).
has_factor(N,L) :- N mod L =:= 0.
has_factor(N,L) :- L * L < N, L2 is L + 2, has_factor(N,L2).
效果很好。
我的逻辑是:测试N是否为素数,如果是,则在列表中添加N,如果不忽略,则测试到N1。在这里,代码:
p
我跟随学习Prolog。
该代码在REPL中加载factorial程序,并从What中得到factorial函数的结果。
?-“factorial.pl”。/* 1.从本地文件*/加载程序
true.
?-清单(阶乘/2)。/* 2.将程序列表到屏幕上*/
factorial(0,1).
factorial(A,B) :-
A > 0,
C is A-1,
factorial(C,D),
B is A*D.
>true.
?-因子(10,什么)。/* 3.计算因子为