要在prolog

时间:2017-04-22 17:33:01

标签: list prolog max minimum

domains
    list = integer*//shows error in swish saying "syntax error operator
                     expected"
    Max = integer
predicates
    maximum_no(list,integer)
clauses
    maximum_no([],Max):-
        write("Maximum No in List is:: ",Max),nl.
    maximum_no([H|T],Max):-
        H>Max,
        N = H,
        maximum_no(T,N).
    maximum_no(L,Max):-
        maximum_no(L,Max).

有人可以告诉我如何解决此错误?我发现代码是正确的,但它仍然说错误? 我还必须编写代码来查找列表的最小数量!!

2 个答案:

答案 0 :(得分:0)

以下单独工作以查找列表的最大值:

% the maximum of a list of one element is this element
maximum_no([X],X).

% the maximum of a list is either the head or the maximum of the tail
% depending on if the head is lower than the max of the tail.
maximum_no([H|T],Max):-
    maximum_no(T,Max),
    H @< Max.
maximum_no([Max|T],Max):-
    maximum_no(T,M),
    M @< Max.

然后,找到列表的最小值不应该太复杂。

答案 1 :(得分:0)

这应该有效。至少要进行适当的更改。

maximum_no([H | T],Max):-     maximum_no(T,MX),     最大值是最大值(H,MX)。

相关问题