Commit 2cee916f authored by Vít Novotný's avatar Vít Novotný
Browse files

added example 2.4.1_6

parent 4b8241b9
% nacteni:
/* ['2.4.1_6.pl']. */
append([],L,L).
append([H|T1],L2,[H|T]) :- append(T1,L2,T).
% demonstracni vypis
% abychom se vyhli varovanim "Redefined static procedure ..."
:- dynamic
write_all_X/3,
start/0.
write_all_X(Goal,X,Name):-
call(Goal),write(' '),write(Name),write(' = '),write(X),nl,fail.
write_all_X(_,_,_).
write_all_XY(Goal,X,XName,Y,YName):-
call(Goal),write(' '),write(XName),write(' = '),write(X),
write(' '),write(YName),write(' = '),write(Y),
nl, fail.
write_all_XY(_,_,_,_,_).
start:-
write('Prace se seznamy - append'),nl,
write('-------------------------------'),nl, nl,
write('vicesmernost predikatu append:'),nl,
write('Vysledek volani "append([a,b],[c,d],L)":'),nl,
write_all_X(append([a,b],[c,d],L), L, 'L'),nl,
write('Vysledek volani "append(X,[c,d],[a,b,c,d ]).":'),nl,
write_all_X(append(X,[c,d],[a,b,c,d ]), X, 'X'),nl,
write('Vysledek volani "append(X,Y,[a,b,c])":'),nl,
write_all_XY(append(X,Y,[a,b,c]), X, 'X', Y, 'Y'),nl.
?-start.
:- retractall(write_all_X/3).
:- retractall(start/0).
#!/usr/bin/env python
# encoding=utf-8 (pep 0263)
def append(xs, ys):
if not xs:
return ys
else:
return [xs[0]] + append(xs[1:], ys)
def append_anyXs(ys, zs):
if ys == zs:
return []
else:
if zs:
return [zs[0]] + append_anyXs(ys, zs[1:])
else:
raise ValueError("ys neni sufixem zs")
def append_anyXsYs(zs):
yield ([], zs)
if zs:
for xs, ys in append_anyXsYs(zs[1:]):
yield ([zs[0]] + xs, ys)
# demonstracni vypis
if __name__ == "__main__":
print("Prace se seznamy - append")
print("-------------------------------\n")
print("vicesmerne implementace funkce append:\n")
print("Vysledek volani append(['a', 'b'], ['c', 'd']): %s\n" % \
append(['a', 'b'], ['c', 'd']))
print("Vysledek volani append_anyXs(['c', 'd'], ['a', 'b', 'c', 'd']): %s\n" % \
append_anyXs(['c', 'd'], ['a', 'b', 'c', 'd']))
print("Vysledek volani append_anyXsYs(['a', 'b', 'c']): %s\n" % \
list(append_anyXsYs(['a', 'b', 'c'])))
Supports Markdown
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment