4.1. Создайте предикат, проверяющий, является ли данная строка палиндромом.
pal([],""):-!.
pal(Str,Newstr):-
Str=[H|T],
pal(T,RT),
append(RT,[H],Newstr).
Работа в прологе
ā% library(win_menu) compiled into win_menu 0.00 sec, 29 clauses
% c:/documents and settings/student/application data/swi-prolog/pl.ini compiled 0.00 sec, 1 clauses
% c:/Documents and Settings/Student/Рабочий стол/aa.pl compiled 0.00 sec, 3 clauses
Welcome to SWI-Prolog (Multi-threaded, 32 bits, Version 6.2.0)
Copyright (c) 1990-2012 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.
1.?- pal("Привет",X), string_to_list(Y,X).
X = [1090, 1077, 1074, 1080, 1088, 1055],
Y = "тевирП".
4.2. Дано предложение найти самое короткое слово в нём.
min(X,Y,X):- X<Y,!.
min(_,X,X).
str_min(Str1, Str2, Str1):-
length(Str1, Length1),
length(Str2, Length2),
min(Length1,Length2,Length1),!.
str_min(_, Str, Str).
list_str_min([],_):-!.
list_str_min(List, Min):-
List=[First|AllWords],
list_str_min(AllWords, MinfromAllWords),
str_min(First, MinfromAllWords, Min).
list_of_words([],[]):-!.
list_of_words(Str, ListofWords):-
append(FirstWord, [32|Tile],Str),!,
list_of_words(Tile, ListofTile),
ListofWords=[FirstWord|ListofTile].
list_of_words(Word, [Word]):-!.
min_from_cent(Str, Min):-
list_of_words(Str, ListofWords),
list_str_min(ListofWords, Min).
Работа в прологе.
% library(win_menu) compiled into win_menu 0.00 sec, 29 clauses
% c:/documents and settings/student/application data/swi-prolog/pl.ini compiled 0.00 sec, 1 clauses
% c:/Documents and Settings/Student/Рабочий стол/lab.pl compiled 0.00 sec, 11 clauses
Welcome to SWI-Prolog (Multi-threaded, 32 bits, Version 6.2.0)
Copyright (c) 1990-2012 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.
For help, use?- help(Topic). or?- apropos(Word).
1?- min_from_cent("Здравству мир, как дела?",X), string_to_list(Y,X).
X = [1082, 1072, 1082],
Y = "как".
Лабораторная работа №6
Каюрин Е.А. 4-Иэ-12
Жили четверо друзей. Звали их Виктор,Павлик,Карл и Альберт. Фамилии друзей такие же, как и имена, и ни у кого их них имя и фамилия не совпадают. Определите Фамилию и имя каждого мальчика, если известно, что фамилия Карла не Виктор, имя мальчика,у которого фамилия Альберт, есть Фамилией мальчика с именем Павлик.
Текст на языке Prolog
get([A|B],A,B).
get([A|B],C,[A|D]):- get(B,C,D).
reh([ [FF,'Albert'],['Pavlic',FF],[A,B],[C,D] ]):-
F=['Bictor','Karl'],
get(F,FF,F1),
get(F1,C,_),
get(F1,B,_),
A='Albert',
D='Pavlic'.
Работа программы в прологе
% library(win_menu) compiled into win_menu 0.00 sec, 29 clauses
% c:/documents and settings/student/application data/swi-prolog/pl.ini compiled 0.00 sec, 1 clauses
% c:/Documents and Settings/Student/Рабочий стол/tt.pl compiled 0.00 sec, 4 clauses
Welcome to SWI-Prolog (Multi-threaded, 32 bits, Version 6.2.0)
Copyright (c) 1990-2012 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.
For help, use?- help(Topic). or?- apropos(Word).
1?- reh(X).
X = [['Bictor', 'Albert'], ['Pavlic', 'Bictor'], ['Albert', 'Karl'], ['Karl', 'Pavlic']];
X = [['Karl', 'Albert'], ['Pavlic', 'Karl'], ['Albert', 'Bictor'], ['Bictor', 'Pavlic']];
false.
Лабораторная работа №5 Каюрин Е. А.4-ИЭ-12
Создать текстовый файл t. Удалить из текста файла t предпоследний элемент.
Текст на прологе.
readfromfile(Name,String):-
ls, see(Name),
get_char(T),
not(T=end_of_file),!,
char_code(T,Code),
String=[Code|OtherCodes],
readfromfile(Name,OtherCodes).
readfromfile(_,[]):-seen.
str_delete(Str,Pos,Count,Result):-
I is Pos-1,
append(Head,Tile,Str),
length(Head,I),
append(Cut,TileofTile,Tile),
length(Cut,Count),
append(Head,TileofTile,Result).
Работа в прологе.
library(win_menu) compiled into win_menu 0.00 sec, 29 clauses
% c:/documents and settings/student/application data/swi-prolog/pl.ini compiled 0.00 sec, 1 clauses
% c:/Documents and Settings/Student/Рабочий стол/lab5.pl compiled 0.02 sec, 4 clauses
Welcome to SWI-Prolog (Multi-threaded, 32 bits, Version 6.2.0)
Copyright (c) 1990-2012 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.
For help, use?- help(Topic). or?- apropos(Word).
readfromfile('t.txt',X), length(X,Z), string_to_list(S,X).
X = [53, 54, 115, 115, 102, 115, 100, 102, 115|...],
Z = 11,
S = "56ssfsdfsd5".
readfromfile('t.txt',X), length(X,Z), string_to_list(S,X), str_delete(X,10,1,Y),string_to_list(Q,Y).
X = [53, 54, 115, 115, 102, 115, 100, 102, 115|...],
Z = 11,
S = "56ssfsdfsd5",
Y = [53, 54, 115, 115, 102, 115, 100, 102, 115|...],
Q = "56ssfsdfs5".
Каюрин Е.А. 4-иэ-12
Базы данных и экспертные системы.
Лабораторная работа №3
Задание1. Найти среднеарифметическое значение чисел, находящихся в узлах дерева.
Текст программы.
count(empty,0).
count(tree(_,empty,empty),0).
count(tree(_,Left,Right),X):-
count(Left,XL),
count(Right,XR),
X is XL+XR+1.
sum1(empty,0).
sum1(tree(X,Left,Right),Y):-
sum1(Left,SL),
sum1(Right,SR),
Y is SL+SR+X.
tree_leavessum(tree(Y,empty,empty),Y):-!.
tree_leavessum(tree(_,Left,Right),Y):-
tree_leavessum(Left,YL),
tree_leavessum(Right,YR),
Y is YL +YR.
tree_avogadro(Tree, Avr):-
count(Tree, Count),
tree_leavessum(Tree,Sum),
sum1(Tree,Sum1),
Avr1=Sum1-Sum,
Avr is Avr1/Count.
main:- T=tree(2,tree(7,empty,empty),tree(3,tree(4,empty,empty),tree(1,empty,empty))),
write(T), nl,
tree_avogadro(T,Avr),
write('Avr='), write(Avr).
Работа на прологе
% library(win_menu) compiled into win_menu 0.00 sec, 29 clauses
% f:/reer.pl compiled 0.00 sec, 10 clauses
Welcome to SWI-Prolog (Multi-threaded, 32 bits, Version 6.2.0)
Copyright (c) 1990-2012 University of Amsterdam, VU Amsterdam
SWI-Prolog comes with ABSOLUTELY NO WARRANTY. This is free software,
and you are welcome to redistribute it under certain conditions.
Please visit http://www.swi-prolog.org for details.
For help, use?- help(Topic). or?- apropos(Word).
1?- main.
tree(2,tree(7,empty,empty),tree(3,tree(4,empty,empty),tree(1,empty,empty)))
Avr=2.5
true