基于序言中的条件原子的拆分列表

Splitting list based on conditional atoms in prolog

我有一个列表

X = [-n,-b,-s,hello,world]

我需要的输出

Z1 = [-n,-b,-s]
Z2 = [hello,world]

如果字符串以 - 开头,它应该是 Z1 列表的一部分,否则是 Z2 列表的一部分。

有人能给我一些基本的直觉来实现这个吗?

使用 library(apply) 和 library(yall) 是立竿见影的:

?- partition([E]>>(E = - _), [-n,-b,-s,hello,world], N, P).
N = [-n, -b, -s],
P = [hello, world].

要在旧的、普通的 Prolog 中实现,请访问列表并'cons'所需列表中的元素:

divide_dashed([], [], []).
divide_dashed([-E|R], [-E|Ds], Ps) :- !, divide_dashed(R, Ds, Ps).
divide_dashed([E|R], Ds, [E|Ps]) :- divide_dashed(R, Ds, Ps).