Это отличные ответы.
Вот еще один. nth0
происходит от library(lists)
.
gimme(List,Len) :- Len >= 2,
Zlen is Len-2,
findall(0, between(1, Zlen, _), Zlist),
between(0, Zlen, Pos1), % we will insert 'a' at Pos1
Pos1n is Pos1+1,
between(Pos1n,Len,Pos2), % we will insert 'b' at Pos2, always after 'a'
nth0(Pos1, Tlist, a, Zlist), % Zlist -morph-> Tlist
nth0(Pos2, List, b, Tlist). % Tlist -morph-> List
?- gimme(L,2).
L = [a, b] ;
false.
?- gimme(L,3).
L = [a, b, 0] ;
L = [a, 0, b] ;
L = [0, a, b] ;
false.
?- gimme(L,4).
L = [a, b, 0, 0] ;
L = [a, 0, b, 0] ;
L = [a, 0, 0, b] ;
L = [0, a, b, 0] ;
L = [0, a, 0, b] ;
L = [0, 0, a, b] ;
false.
?- gimme(L,5).
L = [a, b, 0, 0, 0] ;
L = [a, 0, b, 0, 0] ;
L = [a, 0, 0, b, 0] ;
L = [a, 0, 0, 0, b] ;
L = [0, a, b, 0, 0] ;
L = [0, a, 0, b, 0] ;
L = [0, a, 0, 0, b] ;
L = [0, 0, a, b, 0] ;
L = [0, 0, a, 0, b] ;
L = [0, 0, 0, a, b] ;