Prolog程序从结果列表中排除整数

时间:2017-07-09 05:17:36

标签: list prolog

我需要将列表拆分为大于N且小一个的列表,而忽略任何非整数。它完美地工作,除了当我包含非整数时它抛出异常。任何人都可以看到我做错了吗:

split([],N, [],[]).
split([HeadOrig | TailOrig], N, L1, TailOrig):- 
    \+ integer(HeadOrig), 
    split(TailOrig, N, L1, TailOrig).
split([HeadOrig|TailOrig],N, [HeadOrig|Tail1],L2):-
    integer(HeadOrig), HeadOrig >= N, 
    !,
    split(TailOrig, N, Tail1, L2).
split([HeadOrig | TailOrig], N, L1, [HeadOrig | Tail2]) :- 
    integer(HeadOrig), HeadOrig < N, 
    !,
    split(TailOrig, N, L1, Tail2).

1 个答案:

答案 0 :(得分:1)

问题是该子句中的最后一个术语不应该是TailOrig

split([], _, [],[]).

split([HeadOrig | TailOrig], N, L1, L2):-
    \+ integer(HeadOrig),
    split(TailOrig, N, L1, L2),
    !.

split([HeadOrig|TailOrig], N, [HeadOrig|Tail1], L2):-
    integer(HeadOrig), HeadOrig >= N,
    !,
    split(TailOrig, N, Tail1, L2).

split([HeadOrig | TailOrig], N, L1, [HeadOrig | Tail2]) :-
    integer(HeadOrig), HeadOrig < N,
    !,
    split(TailOrig, N, L1, Tail2).