1 / 22

Chapter 3

Chapter 3. Input and Output. Turbo Prolog Editor. Ctrl + K + B = Start block Ctrl + K + K = End block Ctrl + K + C = Copy block Ctrl + K + V = Move block Ctrl + K + Y = Delete block Ctrl + Y = Delete line. AND, OR, NOT. and use , or use ; not. HELP Command

letitia
Download Presentation

Chapter 3

An Image/Link below is provided (as is) to download presentation Download Policy: Content on the Website is provided to you AS IS for your information and personal use and may not be sold / licensed / shared on other websites without getting consent from its author. Content is provided to you AS IS for your information and personal use only. Download presentation by click this link. While downloading, if for some reason you are not able to download a presentation, the publisher may have deleted the file from their server. During download, if you can't get a presentation, the file might be deleted by the publisher.

E N D

Presentation Transcript


  1. Chapter 3 Input and Output

  2. Turbo Prolog Editor • Ctrl + K + B = Start block • Ctrl + K + K = End block • Ctrl + K + C = Copy block • Ctrl + K + V = Move block • Ctrl + K + Y = Delete block • Ctrl + Y = Delete line Chapter3

  3. AND, OR, NOT and use , or use ; not HELP Command file ?PROLOG open with notepad Chapter3

  4. EX04EX05 is letter predicates isletter(char) clauses /* When applied to characters, '<=' means "alphabetically precedes or is the same as" */ isletter(Ch) :- 'a' <= Ch, Ch <= 'z'. isletter(Ch) :- 'A' <= Ch, Ch <= 'Z'. goal : isletter(‘K’) yes • arithematics < > <= > = + - * / not • char : put ‘....’ when asking Chapter3

  5. Setup makewindow makewindow(1, 120, 35, "Left operand", 2, 5, 5, 25) Location 1 : window number 2 : background color and text color in the window 3 : frame color 4 : text center of the border 5 : row 6 : column 7 : Height 8 : Width cursor(row,column) cursor(25,80) Chapter3

  6. Setup color HOW TO CALCULATE SCREEN ATTRIBUTES. • Choose ONE foreground color and ONE background color. • Add the corresponding integer values below. 3. Add 128 if you want whatever is displayed with that attribute to blink. BACKGROUND-COLORS (Screen)FOREGROUND-COLORS (Characters) -----------------------+---------+----------------+------------ Black 0 Black 0 Blue 16Blue 1 Green 32 Green 2 Cyan 48 Cyan 3 Red 64Red 4 Magenta 80 Magenta 5 Brown 96 Brown 6 White 112 White 7 Grey 8 Light Blue 9 Light Green 10 Light Cyan 11 23 = 16 + 7 Light Red 12 Light Magenta 13 Yellow 14 White (High Intensity) 15 Chapter3

  7. EX14EX01.PRO : makewindow predicates run clauses run :- makewindow(1, 20, 7, "A blue window", 2, 5, 10, 50), write("The characters are red"), nl, makewindow(2, 176, 7, "A light cyan window", 14, 25, 10, 40), write("This window is light cyan and the "), write("letters are black and blink."), nl, write("Please type an integer to exit."), nl, readint(_), removewindow, write("Please type an integer to exit."), nl, readint(_), removewindow. Chapter3

  8. Window1.PRO : EX04EX03.PRO • predicates • run(char) • goal • run(X). /* This is a internal goal. */ • clauses • run(X) :- • makewindow(1, 7, 7, " Hello World Program ",0,0,25,80), • write("Hello World (first)"), • readchar(X), • removewindow. • run(X) :- • write("hello World (second)"), • readchar(X). Chapter3

  9. Window2.PRO • predicates • run(char) • goal • run(X). /* This is a internal goal. */ • clauses • run(X) :- • makewindow(1,7,7,"hello",0,0,25,80), • write("hello World (second)"), • readchar(X). • run(X) :- • makewindow(1, 7, 7," Hello World Program ", 0, 0, 25, 80), • write("Hello World (first)"), • readchar(X), • removewindow. Chapter3

  10. Window3.PRO : show 2 windows • predicates • run1(char) • run2(char) • goal • run1(X), • run2(Y). /* This is a internal goal. */ • clauses • run2(X) :- • makewindow(1,7,7,"hello",0,0,25,80), • write("hello World (second)"), • readchar(X). • run1(X) :- • makewindow(1, 7, 7," Hello World Program ", 0, 0, 25, 80), • write("Hello World (first)"), • readchar(X), • removewindow. Chapter3

  11. EX14EX02.pro : SUM 2 NUMBERS goal: start predicates start run(integer) do_sums set_up_windows clear_windows clauses start :- set_up_windows, do_sums. set_up_windows :- makewindow(1, 7, 7, "", 0, 0, 25, 80), makewindow(1, 7, 7, "Left operand", 2, 5, 5, 25), makewindow(2, 7, 7, "", 2, 35, 5, 10), nl, write(" PLUS"), makewindow(2, 7, 7, "Right operand", 2, 50, 5, 25), makewindow(3, 7, 7, "Gives", 10, 27, 5, 25), makewindow(4, 7, 7, "", 17, 22, 5, 35). do_sums:- run(_),clear_windows, do_sums. Chapter3

  12. EX14EX02.pro : SUM 2 NUMBERS (cont.) run(Z) :- shiftwindow(1), cursor(2, 1), readint(X), shiftwindow(2), cursor(2, 10), readint(Y), shiftwindow(3),Z=X+Y, cursor(2, 10), write(Z), shiftwindow(4), write(" Please press the space bar"), readchar(_). clear_windows :- shiftwindow(1), clearwindow, shiftwindow(2), clearwindow, shiftwindow(3), clearwindow, shiftwindow(4), clearwindow. Chapter3

  13. goal: ownes(X,Y) LISTEX01.pro domains thing = book(title,author) ; car(name); shoe author = author(firstname,lastname) person,title,name,firstname,lastname,shoe = symbol predicates ownes(person,thing) clauses ownes(siri,book("AI book",author(tom,brook))). ownes(siri,car(honda)). ownes(siri,shoe). Chapter3

  14. EX12EX07.PRO : Read Person Information domains person = p(name, age, telno, job) age = integer telno, name, job = string predicates readperson(person) run goal run. clauses readperson(p(Name, Age, Telno, Job)) :- write("Which name ? "), readln(Name), write("Job ?"), readln(Job), write("Age ?"), readint(Age), write("Telephone no ?"), readln(Telno). run:- readperson(P), nl, write(P), nl, nl, write("Is this compound object OK (y/n)"),readchar(Ch), Ch='y'. run :- nl, nl, write("Alright, try again"), nl, nl, run. Chapter3

  15. EX07EX02.PRO : Typewriter /* Uses repeat to keep accepting characters and printing them until the user presses Enter. */ predicates repeat typewriter clauses repeat. repeat :- repeat. typewriter :- repeat, readchar(C), /* Read a char, bind C to it */ write(C), char_int(C,13). /* Is C = ASCII 13? */ goal: typewriter Chapter3

  16. EX05EX09 Interactive window predicates repeat action(integer,string) test(string) goal makewindow(1,7,7,"interaction window",0,2,11,43), repeat, shiftwindow(1), clearwindow, write("0. Enter 0 to end\n"), write("1. Enter 1 to create a window and input\n a new string\n"), write("2. Enter 2 to remove the window and text\n"), write("3. Enter 3 to write to existing window\n\n"), write("Selection? "), readint(Int),nl, action(Int,Text), Int = 0,!, /* this cut will prevent backtracking even if you have not created a string */ test(Text). clauses action(0,"EXIT"):- !, /* this cut prevents Turbo Prolog from looking at other options. */ exit. Chapter3

  17. EX05EX09 Interactive window action(1,Str):- existwindow(2), write("You have a window that already exists.\n"), write("Do you wish to clear it.(y,n) "), readchar(Ans),!, Ans='y', /* If you answer yes to the question this cut prevents the backtracking to the second action(1) clause. */ nl, shiftwindow(2), clearwindow, write("Enter your string\n"), readln(Str). action(1,Str):- !, /* this cut prevents Turbo Prolog from looking at other options. */ nl, makewindow(2,7,7," simple window control ", 12, 3, 12, 40), write("Enter your string\n"), readln(Str). action(2,"window removed"):- existwindow(2), !, /* If the window has been input, this cut will prevent the second action(2) clause from executing */ shiftwindow(2), removewindow, clearwindow. ...... action(3,Str):- existwindow(2),!, shiftwindow(2), clearwindow, write("Enter your string\n"), readln(Str). ....... action(_,"ERROR"):- write("not a valid option\n"), write("press any key to continue"). test(Text):- write(Text). repeat. repeat:-repeat. Chapter3

  18. EX16EX02.PRO : show time predicates timer clauses timer:- time(H1,M1,S1,D1),nl, write("Start time is: ",H1,":",M1,":",S1," and ",D1,"/100 sec"),nl, /* This is the activity that is being timed */ system("dir *.*"), time(H2,M2,S2,D2), Time = (D2-D1) + 100 * ((S2 - S1) + 60 * ((M2 - M1) + 60 * (H2 - H1))), write("Elapsed time: ",Time,"/100 sec"),nl, time(H3,M3,S3,D3), write("The time now is: ",H3,":",M3,":",S3," and ",D3,"/100 sec"),nl. goal makewindow(1,7,7," Timer ",8,10,12,60), write("Press any key to start"), readchar(_),timer. Chapter3

  19. We can set the program to run automatic by using the command at ..... goal EX05EX05 lives(zebra, on_land). lives(frog, on_land). lives(frog,in_water). lives(shark,in_water). can_swim(Y) :- type(X, animal) , is_a(Y, X) , lives(Y,in_water). predicates type(symbol, symbol) is_a(symbol, symbol) lives(symbol, symbol) can_swim(symbol) goal can_swim(What) , write("A ",What," can swim.\n"). clauses type(ungulate, animal). type(fish, animal). is_a(zebra, ungulate). is_a(herring, fish). is_a(shark, fish). • write(“..............” ) • write(“\n.......”,Variable ,”..... \n\n\n”) Chapter3

  20. Fail : use tomake the rule not true : prolog will find all of the solutions. cswim1 is_a(zebra, ungulate). is_a(herring, fish). is_a(shark, fish). is_a(tuna, fish). lives(zebra, on_land). lives(frog, on_land). lives(frog, in_water). lives(shark, in_water). lives(herring, in_water). lives(tuna, in_water). can_swim(Y) :- type(X, animal) , is_a(Y, X) , lives(Y,in_water). predicates type(symbol, symbol) is_a(symbol, symbol) lives(symbol, symbol) can_swim(symbol) goal can_swim(What) , write("A ",What," can swim.\n"),fail. clauses type(ungulate, animal). type(fish, animal). Chapter3

  21. We can set the program to run automatic by using the command at ..... goal can_swim lives(zebra, on_land). lives(frog, on_land). lives(frog,in_water). lives(shark,in_water). lives(tuna,in_water). can_swim(Y) :- type(X, animal) , is_a(Y, X) , lives(Y,in_water). run :- write("\n Hello \n"), can_swim(What), write("A ", What, " can swim.\n"), fail. predicates type(symbol, symbol) is_a(symbol, symbol) lives(symbol, symbol) can_swim(symbol) run. goal run. clauses type(ungulate, animal). type(fish, animal). is_a(zebra, ungulate). is_a(tuna, fish). is_a(shark, fish). Chapter3

  22. EX05EX06 father • Some predicate may not have attributes Ex :everybody domains name = symbol predicates father(name, name) everybody clauses father(leonard, katherine). father(carl, jason). father(carl, marilyn). everybody :- father(X, Y), write(X, " is ", Y, "'s father\n"), fail. Chapter3

More Related