
Опытный
 
Профиль
Группа: Участник
Сообщений: 346
Регистрация: 3.11.2007
Репутация: нет Всего: 4
|
Просто не понимаю как сделать | Цитата | without using the append relation
|
Подскажите пожалуйста решения вот этих задачек, буду очень благодарен. В прологе почти 0 ( | Код | 2. Write the following relations without using the append relation. Use one fact and one rule in a) and b). a. prefix(X,Y) List X is a prefix of list Y. b. suffix(X,Y) List X is a suffix of list Y. c. sublist(X,Y) List X is a sublist of list Y, i.e sublist([2,3], [1,2,3,4]). You can use the prefix relation in the solution. Your code will be tested using the queries like: prefix([1,2],[1,2,3,4]). prefix(X,[1,2,3]). suffix([3,4],[1,2,3,4]). suffix(X,[1,2,3]). sublist([2,3], [1,2,3,4]). sublist(X,[1,2,3]). 3. (20%) Write the following relations. You are not allowed to use built-in relations, unless it is explicitly stated. a. sum(L,S) S is the sum of the integers in list L. Use the built-in relation plus(X,Y,Z), e.g. plus(1,2,3). b. double(X,Y) Each element in list X occurs exactly twice in list Y. c. interval(X,Y,L) L is a list whose elements are integers between X and Y. d. numbers(L) L is a list whose elements only include the single integers 0,1,…9. Your code will be tested using queries like: sum([1,2,3],6). sum([1,2,3],7). sum([1,2,3,4,5],X). double([1,2],[1,1,2,2]). double([1,2],[1,1,2,2,2]). double([1,2,3],X). interval(1,3,[1,2,3]). interval(4,7,X). numbers([1,3,5,7,9]). numbers([2,a,b,8]). 4. Write the following relations. You are not allowed to use built-in relations, unless it is explicitly stated. a. random(X,Y) Y is a random integer between 0 and X. Use the built-in random/1 and round/1 relations. b. remove(X,L,K,R) R is a list obtained by removing the K-th element from the list L. X is the removed element. c. select(L,K,R) R is a list obtained by selecting K elements at random from list L. Use the built-in length/2 relation, the random/2 relation from a) and the remove/4 relation from b). Your code will be tested using the queries like: random(8,Y). remove(X,[a,b,c,d],2,R). remove(c,[a,b,c,d],3,R). select([a,b,c,d,e,f,g,h],3,L). select([a,b,c,d,e,f,g,h],5,L). 5. Implement mergesort by writing the following relations: a) mymerge(X, Y, Z) Z is a sorted list obtained by merging the sorted lists X and Y. b) mysplit(X,Y,Z) List X is splitted into two lists Y and Z. Y contains elements no. 1,3,5… from X, and Z contains elements no. 2,4,6… from X. c) mysort(X,Y) Y is a sorted version of list X (in ascending order). Your code will be tested using the queries like: mymerge([1,3,5],[2,4],[1,2,3,4,5]). mysplit([1,2,3,4,5],[1,3,5],[2,4]). mysplit([1,2,3,4,5],X,Y). mysort([5,2,8,4,7,3,1,9,6], X).
|
|