程序代写 Declarative Programming

Declarative Programming
Assignment 1
ÊÚ¿ÎÀÏʦ:Ethan

Copyright By PowCoder代写 加微信 powcoder

– replace(E1, L1, E2, L2)
– This holds when list L1 is the same as L2, except that in one place
where L1 has the value E1, L2 has E2.
– Note that only one occurrence of E1 is replaced.
1. replace(2,[1,2,3,4],5,X)shouldhaveonlythesolutionX=[1,5,3,4].
2. replace(2,[1,2,3,2,1],5,X) should backtrack over the solutions X = [1,5,3,2,1] and X = [1,2,3,5,1].
3. replace(2,X,5,[1,5,3,5,1])shouldbacktrackoverthesolutionsX=[1,2,3,5,1]andX=[1,5,3,2,1].
4. replace(X,[a,b,c,d],Y,[a,e,c,d]) should have only the solution X = b, Y = e.
5. replace(X,[1,2,3,2,1],Y,[1,5,3,5,1])shouldhavenosolutions(itshouldfail).
°æȨËùÓеÁ°æ±Ø¾¿
±¨ÃûÁªÏµÎÒÃÇ

– replace(E1, L1, E2, L2 )
– L=[X|Xs].
– replace(E1, [X | Xs], E2, [Y | Ys])
– E1->E2 – (3) (a)
°æȨËùÓеÁ°æ±Ø¾¿
±¨ÃûÁªÏµÎÒÃÇ

– replace(E1, [ X | Xs ], E2, [ Y | Ys ]) –
– X \= E1 , L2 ²»½øÐÐÌæ»»£¬ÕÕ³­ – Y=X
– Xs \= YsÔÙ¼ì²âXsºÍYs
– replace(E1, [ X | Xs ], E2, [ X | Ys ]) :-
°æȨËùÓеÁ°æ±Ø¾¿
±¨ÃûÁªÏµÎÒÃÇ

– replace(E1, [ X | Xs ], E2, [ Y | Ys ]) –
– X \= E1 , L2 ²»½øÐÐÌæ»»£¬ÕÕ³­ – Y=X
– Xs \= YsÔٵݹé¼ì²âXsºÍYs
– replace(E1, [ X | Xs ], E2, [ X | Ys ]) :- replace(E1, Xs, E2, Ys).
°æȨËùÓеÁ°æ±Ø¾¿
±¨ÃûÁªÏµÎÒÃÇ

– replace(E1, [ X | Xs ], E2, [ Y | Ys ]) –
– X=E1,½øÐÐÌæ»»
– Í£Ö¹¼ì²â
– replace(E1, [ E1 | Xs ], E2, [ E2 | Xs ]) °æȨËùÓеÁ°æ±Ø¾¿
±¨ÃûÁªÏµÎÒÃÇ

Replace replace(E1, L1, E2, L2)
– replace(E1, [ E1 | Xs ], E2, [ E2 | Xs ]).
– replace(E1, [ X | Xs ], E2, [ X | Ys ]) :- replace(E1, Xs, E2, Ys).
– replace(E1, [], E2, []) ???
– replace(_, [], _, [])
– Replace – Ignore
123456 12a456
123456 12a456
°æȨËùÓеÁ°æ±Ø¾¿
±¨ÃûÁªÏµÎÒÃÇ

– zip(As, Bs, ABs)
– This holds when As, Bs, and ABs are lists of the same length, and each element of ABs is a term of the form A-B where A is the corresponding element of As and B is the corresponding element of Bs.
1. zip([1,2,3,4],[a,b,c,d],L)shouldhaveonlythesolutionL=[1-a,2-b,3-c,4-d]].
2. zip(X,Y,[1-a,2-b,3-c,4-d])shouldhaveonlythesolutionX=[1,2,3,4],Y=[a,b,c,d].
3. zip([1,2,3,4],Y,[1-a,2-b,3-c,4-d])shouldhaveonlythesolutionY=[a,b,c,d].
4. zip(X,[a,b,c,d],[1-P,2-Q,3-R,4-S]) should have only the solution X=[1,2,3,4], P=a, Q=b, R=c, S=d.
5. zip([1,2,3],[a,b,c,d],L)shouldfail.
°æȨËùÓеÁ°æ±Ø¾¿
±¨ÃûÁªÏµÎÒÃÇ

– zip(As, Bs, ABs)
– L=[X|Xs].
– zip([A | As], [B | Bs] , [AB | ABs])
°æȨËùÓеÁ°æ±Ø¾¿
±¨ÃûÁªÏµÎÒÃÇ

– zip([A | As], [B | Bs] , [AB | ABs]) –
– AºÍB ×éºÏ³ÉA-B
– As BsÔÙÉú³ÉABs
– zip([A | As], [B | Bs] , [A-B | ABs])
°æȨËùÓеÁ°æ±Ø¾¿
±¨ÃûÁªÏµÎÒÃÇ

– zip([A | As], [B | Bs] , [AB | ABs]) –
– AºÍB ×éºÏ³ÉA-B
– As BsÔÙÉú³ÉABs
– zip([A|As],[B|Bs],[A-B|ABs]):- zip(As, Bs, ABs).
°æȨËùÓеÁ°æ±Ø¾¿
±¨ÃûÁªÏµÎÒÃÇ

Zip zip(As, Bs, ABs). – Zip([],[],[]).
– zip([A|As],[B|Bs],[A-B|ABs]):- zip(As, Bs, ABs).
°æȨËùÓеÁ°æ±Ø¾¿
±¨ÃûÁªÏµÎÒÃÇ

– sublist(Xs, Ys)
– This holds when Xs is a list containing some of the elements of Ys, in the same order they appear in the list Ys. This should work whenever Ys is a proper list.
1. sublist([a,c,e],[a,b,c,d,e])shouldsucceed.
2. sublist([a,e,c],[a,b,c,d,e])shouldfail.
3. sublist([a,X,d],[a,b,c,d,e])shouldhavethetwosolutionsX=bandX=c.
4. sublist(X,[a,b,c])shouldhavetheeightsolutionsX=[];X=[c];X=[b];X=[b,c];X=[a];X=[a,c];X=[a,b]; and X=[a,b,c].
°æȨËùÓеÁ°æ±Ø¾¿
±¨ÃûÁªÏµÎÒÃÇ

– sublist(Xs, Ys)
– L=[X|Xs].
– sublist([X | Xs], [Y | Ys])
°æȨËùÓеÁ°æ±Ø¾¿
±¨ÃûÁªÏµÎÒÃÇ

– sublist([X | Xs], [Y | Ys]) –
– X \= Y , ËùÒÔY²»ÊÇÆðʼ½Úµã
– ÔÙ¼ì²â[X|Xs] ºÍ YsÊÇ·ñÆ¥Åä
– sublist([X | Xs], [Y | Ys]) :- sublist([X | Xs], Ys)
– sublist(Xs, [_| Ys]) :- sublist(Xs, Ys)
°æȨËùÓеÁ°æ±Ø¾¿
±¨ÃûÁªÏµÎÒÃÇ

– sublist([X | Xs], [Y | Ys]) –
– X=Y,ËùÒÔYÊÇÆðʼ½Úµã
– ÔÙ¼ì²â Xs ºÍ YsÊÇ·ñÆ¥Åä
– sublist([X | Xs], [X | Ys]) :- sublist(Xs, Ys)
°æȨËùÓеÁ°æ±Ø¾¿
±¨ÃûÁªÏµÎÒÃÇ

– sublist([X | Xs], [Y | Ys]) –
– [X|Xs] = [] , [Y|Ys]ÈÎÒâÆ¥Åä
– sublist([], [Y | Ys]).
– sublist([], Ys).
°æȨËùÓеÁ°æ±Ø¾¿
±¨ÃûÁªÏµÎÒÃÇ

Sublistsublist(Xs,Ys)
– sublist([], _).
– sublist([X|Xs], [X|Ys]) :- sublist(Xs, Ys).
– sublist(Xs, [_|Ys]) :- sublist(Xs, Ys).
2 3 4 5 123456
2345 123456
2345 123456
– Boundary
– Start node
°æȨËùÓеÁ°æ±Ø¾¿
±¨ÃûÁªÏµÎÒÃÇ

程序代写 CS代考 加微信: powcoder QQ: 1823890830 Email: powcoder@163.com