2017-04-13 56 views
0

我想实现一个函数,该列表使用一定宽度的子列表。例如:如何制作具有一定宽度的列表的列表

?- list_to_llists([w,w,w,w],2,LL). %1 
LL = [[w, w], [w, w]] ; 
false. 
?- list_to_llists([w,w,c,l,r,w,c,c,w,w,w,w],3,LL). %2 
LL = [[w, w, c], [l, r, w], [c, c, w], [w, w, w]] ; 
false. 
?- list_to_llists([w,w,w,w],3,LL). %3 
LL = [[w, w, w]] ; 
false. 

sublist(I1,I2,L,Sub) :- 
    sublist2(I1,I2,L,[],Sub). 

sublist2(I1,I2,L,Sub,Sub):- 
    length(Sub,N), 
    N\=0, 
    I1>I2. 
sublist2(I1,I2,L,Sub,Sub2):- 
    I1<I2, 
    nth0(I1,L,X), 
    I3 is I1+1, 
    append(Sub,[X],Z), 
    sublist2(I3,I2,L,Z,Sub2). 
sublist2(A,B,L,Sub,Sub2):- 
    B=A, 
    nth0(A,L,X), 
    NewA is A+1, 
    append(Sub,[X],Z), 
    sublist2(NewA,B,L,Z,Sub2). 


list_to_llists(L,W,LLists):- 
    length(L,X), 
    X=<W, 
    LLists=[L]. 

list_to_llists2([],W,LLists,A):- LLists=A . 
list_to_llists2(L,W,LLists,A):- 
    P is W-1 , 
    sublist(0,P,L,Result), 
    append([Result],A,U), 
    append(Result,K,L), 
    list_to_llists2(K,W,LLists,U). 

list_to_llists(L,W,F):- 
    list_to_llists2(L,W,R,[[]]). 

但病例2和3完全不

工作其次,我想实现一个函数,它接受某些事实,并把它们放在L,其中L是一个列表,但我有使用list_to_lists做出大号样的名单列表清单(这是一个地图

例如(测试用例):

?- length(L,100),ensure_hints(L, [at(3, 5, c), at(5, 0, w), at(9, 6, c)],10,10). 
L = [_G1699, _G1702, _G1705, _G1708, _G1711, w, _G1717, _G1720, _G1723, _G1726, 
_G1729, _G1732, _G1735, _G1738, _G1741, _G1744, _G1747, _G1750, _G1753, _G1756, 
_G1759, _G1762, _G1765, _G1768, _G1771, _G1774, _G1777, _G1780, _G1783, _G1786, 
_G1789, _G1792, _G1795, _G1798, _G1801, _G1804, _G1807, _G1810, _G1813, _G1816, 
_G1819, _G1822, _G1825, _G1828, _G1831, _G1834, _G1837, _G1840, _G1843, _G1846, 
_G1849, _G1852, _G1855, c, _G1861, _G1864, _G1867, _G1870, _G1873, _G1876, 
_G1879,_G1882, _G1885, _G1888, _G1891, _G1894, _G1897, _G1900, _G1903, c, 
_G1909, _G1912,_G1915, _G1918, _G1921, _G1924, _G1927, _G1930, _G1933, _G1936, 
_G1939, _G1942, _G1945, _G1948, _G1951, _G1954, _G1957, _G1960, _G1963, _G1966, 
_G1969,_G1972, _G1975, _G1978, _G1981, _G1984, _G1987, _G1990, _G1993, _G1996]; 
false 
?- length(L,9),ensure_hints(L, [at(1, 2, c), at(0, 1, l)],3,3). 
L = [_G1589, _G1592, _G1595, l, _G1601, _G1604, _G1607, c, _G1613] ; 
false. 
?- length(L,9),ensure_hints(L, [at(1, 2, c), at(0, 5, l)],3,3). 
false. 

,但它不为我工作

我的代码:

ensure_hints(L,Hints,W,H):- 
    list_to_llists(L,W,C), 
    Hints=[H|T], 
    H=at(X,Y,O), 
    nth0(X,L,Z), 
    nth0(Y,Z,O), 
    ensure_hints(L,T,W,H). 

回答

0

这里有一个简单的解决方案:

list_to_llists(List, Len, [H|T]) :- 
    length(H, Len), 
    append(H, Rest, List), 
    !, 
    list_to_llists(Rest, Len, T). 
list_to_llists(_, _, []). 

简单去除长度Len的子列表直到没有完成子列表(看起来你想忽略不完整的子列表)。

你可以用它http://swish.swi-prolog.org/p/OqfzwRoe.pl

+1

这里打球,目标'list_to_llists(列表,莱恩,[])'是'所有List','Len'真。 – false

相关问题