在prolog中列出的列表元素

时间:2017-02-25 15:59:29

标签: prolog

我想对这样一个列表的元素求和:

sum([1,[2,3],4],S).

我用过但我遇到了问题:

sum([],0).
sum([T|R],M) :- sum(R,S), M is T+S.

我收到以下错误:

ERROR: is/2: Type error: `[]' expected, found `[2,3]' (a list) ("x" must hold one character)

3 个答案:

答案 0 :(得分:2)

问题是,如果T是列表,则无法添加T.如果T是列表,您可以使用is_list/1轻松解决:

sum([],0).
sum([T|R],M) :- (is_list(T) -> sum(T,N1),sum(R,S), M is N1+S
                 ; sum(R,S), M is T+S ).

示例:

?- sum([1,[2,3],4],S).
S = 10.

?- sum([1,2,3,4],S).
S = 10.

?- sum([1,[2],[3],4],S).
S = 10.

?- sum([1,[[2],[3]],4],S).
S = 10.

更好的方法是使用CLPFD:

:- use_module(library(clpfd)).

sum([],0).
sum([[]],0).
sum([T|R],M) :- (is_list(T) -> sum(T,N1),sum(R,S), M #= N1+S
                 ; sum(R,S), M #= T+S ).

现在您可以查询更常见的问题,例如:

?- sum(L,N).
L = [],
N = 0 ;
L = [[]],
N = 0 ;
L = [N],
N in inf..sup ;
L = [N, []],
N in inf..sup ;
L = [_1836, _1842],
_1836+_1842#=N ;
L = [_1842, _1848, []],
_1842+_1848#=N ;
L = [_2142, _2148, _2154],
_2142+_2192#=N,
_2148+_2154#=_2192 ;
L = [_2148, _2154, _2160, []],
_2148+_2204#=N,
_2154+_2160#=_2204 ;
L = [_2448, _2454, _2460, _2466],
_2448+_2504#=N,
_2454+_2528#=_2504,
_2460+_2466#=_2528 ;

继续......

答案 1 :(得分:1)

另一种方法:

% Old code, with vars renamed:
%sum([], 0).
%sum([Num|Tail], TotalSum) :- sum(Tail, TailSum), TotalSum is Num + TailSum.

% New code:
sum([], 0).
sum([Elem|Tail], TotalSum) :- 
    sum(Elem, ElemSum),
    sum(Tail, TailSum),
    TotalSum is ElemSum + TailSum.
sum(Num, Num).

演示:http://swish.swi-prolog.org/p/cmzcsXrJ.pl

答案 2 :(得分:0)

你可以在Swi-Prolog中使用flatten / 2 plus foldl / 4或sum_list / 2库谓词:

% flatten/2 + foldl/4 based implementation
sum_nested_list(List, Sum) :-
    flatten(List, Flat),
    foldl(plus, Flat, 0, Sum).

% flatten/2 + sum_list/2 based implementation
sum_nested_list(List, Sum) :-
    flatten(List, Flat),
    sum_list(Flat, Sum).