【发布时间】:2015-01-20 13:50:00
【问题描述】:
我想将通过整数分隔的单词列表拆分为列表列表。
示例查询和预期结果:
?- separatewords([h,e,l,l,o,1,o,v,e,r,3,t,h,e,r,e], X).
X = [[h,e,l,l,o],[o,v,e,r],[t,h,e,r,e]].
我已经实现了以下几点: 将列表拆分为第一个整数之前的一个列表和第一个整数之后的一个列表:
带有结果的示例查询:
?- take1word([h,e,l,l,o,1,o,v,e,r,3,t,h,e,r,e], X, Y).
X = [h,e,l,l,o], Y = [o,v,e,r,3,t,h,e,r,e]. % OK
我的代码:
take1word([H|T],[],T) :-
integer(H).
take1word([H|T],[H|Hs],Y) :-
( float(H), take1word(T,Hs,Y)
; atom(H), take1word(T,Hs,Y)
).
为了分隔单词,我的代码如下:
separatewords([],[]).
separatewords([H|T],L) :- separatewords(T,[take1word([H|T],)|L]).
结果它只给我false,但我不知道,我做错了什么。
【问题讨论】:
标签: list recursion split prolog