Slide 1

Slide 1 text

jgs CSE 240 Introduction to Programming Languages Lecture 25: Expressions in Prolog Dr. Javier Gonzalez-Sanchez [email protected] javiergs.engineering.asu.edu | javiergs.com PERALTA 230U Office Hours: By appointment

Slide 2

Slide 2 text

jgs Expressions

Slide 3

Slide 3 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 3 jgs Arithmetic Queries ?- X is 3 + 5. X=8 ?- X is 3 mod 2. X=1 ?- X is 3 mod 2 - 1 * 5 / 9. X=0.4444444444444444 ?- X is 5+4, Y is 5-4. X=9 Y=1 ?- X is 5*4, Y is 2^3. X=20 Y=8

Slide 4

Slide 4 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 4 jgs Arithmetic Queries ?- X is 234556^100. X = 105841181316488988298177591518 4... ?- X is 5/3. X=1.66666667 ?- X is sqrt(3),Y is 3^0.5. X=Y Y=1.7320508075688772 ?- X is 8 mod 3. X=2 ?- Y is 10^3 * (1+1) + 3. Y=2003

Slide 5

Slide 5 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 5 jgs Arithmetic Queries ?- X is 10+5. X = 15 ?- 6 is 10+5. false ?- 15 is 10+5. true ?- 10+5 is X. Error: Arguments are not sufficiently instantiated ?- 10+5 is 10+5. false

Slide 6

Slide 6 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 6 jgs Arithmetic Queries § X is 5 /3, X = 1. false § X is 5 /3, Y = 1. X=1.666666667, Y=1 § X is 5 /3; X = 1. X = 1.6666666667 X = 1

Slide 7

Slide 7 text

jgs Rules and Recursion

Slide 8

Slide 8 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 8 jgs Rules as functions § doSomething(A,B,P) :- P is A + B. ?-doSomething(3,2,P). ?-doSomething(3,2,10).

Slide 9

Slide 9 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 9 jgs Recursion | Factorial % this is fact factorial(0, 1). % this is a rule % the factorial of F is N*F1 % if N>0 and % N1 is N-1 and % the factorial of N1 is F1 factorial(N, F) :- N>0, N1 is N - 1, factorial(N1, F1), F is N * F1. ?- factorial (3, W). W=6 1. factorial(3, W) apply rule 2 2. 3>0, N1 is 3-1, factorial (N1, F1), F is 3 *F1 solve the individual parts true, 2 is 3-1, factorial (2, F1), F is 3*F1 apply rule 2 again 3. 2>0, N2 is 2-1, factorial (N2, F2), F1 is 2 * F2 solve the individual parts true, 1 is 2-1, factorial (1, F1), F1 is 2*F2 apply rule 2 again 4. 1>0, N3 is 1-1, factorial (N3, F3), F2 is 2 * F3 solve the individual parts true, 0 is 1-1, factorial (0, F3), F2 is 1*F3 apply rule 1 5. factorial (0, 1) F3 is 1 6. Go back, replace F3 to get F2, then F2 to get F1, then F1 to get F.

Slide 10

Slide 10 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 10 jgs Recursion | Factorial

Slide 11

Slide 11 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 11 jgs Recursion | Fibonacci fib(0, 0). fib(1, 1). fib(X, Y) :- X > 1, X2 is X – 2, fib(X2, Y2), X1 is X – 1, fib(X1, Y1), Y is Y1 + Y2.

Slide 12

Slide 12 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 12 jgs Recursion | Fibonacci

Slide 13

Slide 13 text

jgs Final Exam Review

Slide 14

Slide 14 text

jgs The following slides shows some examples related to some topics This is NOT a comprehensive list of topics Topics in the exam can be found Weeks 1 to 8

Slide 15

Slide 15 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 15 jgs Topics § Programming Languages Concepts § C § C++ § LISP § Prolog 2 or 5 questions per topic. Most of them involve programming

Slide 16

Slide 16 text

jgs Test Yourselves C/C++

Slide 17

Slide 17 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 17 jgs C recursion #include void fun(int x) { if (x>0) { printf("%d", x); fun(x-1); printf("%d", x); } } int main() { fun(5); }

Slide 18

Slide 18 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 18 jgs C/C++ pointers § What delete expression correspond to the expression ptr=new int[100]

Slide 19

Slide 19 text

jgs Test Yourselves Lisp

Slide 20

Slide 20 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 20 jgs LISP § What is the output of the following code? (let ((x '(1 2)) (y '(9 10))) (print (+ (first x) (first y)) ) )

Slide 21

Slide 21 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 21 jgs LISP 1. Create a function that receives a list and return the lowerst value 2. Create a function that receives a list and an integer and return “true” if the number exist in the list 3. Create a function that receives a list and return “true” if the list contains a palindrome. such as (a b c d e d c b a) 4. Create a variable in Lisp to store a tic-tac-toe, such as x E x 0 X 0 E x E

Slide 22

Slide 22 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 22 jgs LISP 5. Create a function that receives the variable with the tic-tac-toe and return “true” if there is an empty space (an “E” letter). 6. The following list represent a tree '(a (b () ()) (c (d () () ) (e () ()) ) ) Create a function (recursive) that traverse the tree and print all the elements as A B C D E. 7. What is printed by the following code? (setf route '(boston cambridge lincoln concord)) (print (first (rest route)))

Slide 23

Slide 23 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 23 jgs LISP 8. What is the output of the following code? (let ((x '(1 2)) (y '(9 10))) (print (+ (first x) (first y)) ) ) 9. What is printed by the following code? (defun fun(n) (if (= n 0) () (fun (- n 1)) ) n ) (print (fun 5)) 10. What is equivalent code in C++ for the following LISP statement (if (< 1 2) (+ 3 4) (- 5 6) )

Slide 24

Slide 24 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 24 jgs LISP 1. A function test-min that returns the lower value in a List of numbers (test-min L) 2. A function test-average that returns the average of all values in a List of numbers (test-average L) 3. A function test-fibonacci that prints the first n elements of the fibonacci series (test-fibonacci n) 4. A recursive function that returns the leftmost even element in a list of numbers (test-even L) 5. A functions that traverse a list and returns how many of its elements are 0 (test-count-zeros L) 6. A recursive function that return the N member of a list L (assuming that the elements are numbered from zero onwards): (test-get n L) 7. A recursive function that returns non-NIL if x is a member of the list L. (test-is-in-list x L)

Slide 25

Slide 25 text

jgs Test Yourselves Prolog

Slide 26

Slide 26 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 26 jgs Q1 What is the predicate logic expression equivalent to "Charlie owns a computer" owns(charlie, computer).

Slide 27

Slide 27 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 27 jgs Q2 instructor(john,cs365). instructor(mary,cs311). enrolled(danielle,cs365). enrolled(danielle,cs446). § Which query provides a list of all the students enrolled in cs365? enrolled(Who, cs365).

Slide 28

Slide 28 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 28 jgs Q3 If weather is cold, everyone likes it. Write this statement as a Prolog clause. likes(X, weather) :- cold(weather). %notice “weather” in lowercase

Slide 29

Slide 29 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 29 jgs Q5 § Given the following facts § cat(tom). § black_spots(tom). § dog(pluto). § white_spots(pluto) § Which rule defines in Prolog the following sentence: § "mary owns a Pet if it is a cat and it has black spots" owns(mary, Pet):- cat(Pet), black_spots(Pet).

Slide 30

Slide 30 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 30 jgs Q6 Which rule defines in Prolog the following sentence: "If someone owns something, he loves it" loves(Who, What):-owns(Who, What).

Slide 31

Slide 31 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 31 jgs Q7 1 2 3 4

Slide 32

Slide 32 text

Javier Gonzalez-Sanchez | CSE240 | Spring 2020 | 33 jgs Test Yourselves § A recursive function rec is defined as follows Write a set of rules in Prolog to define the solution to this function. ï î ï í ì ³ - - + - = £ = 3 ) 3 ( * ) 2 ( ) 1 ( 2 1 1 0 ) ( n n rec n rec n rec n n n rec

Slide 33

Slide 33 text

jgs CSE 240 Introduction to Programming Languages Javier Gonzalez-Sanchez, Ph.D. [email protected] Fall 2021 Copyright. These slides can only be used as study material for the class CSE240 at Arizona State University. They cannot be distributed or used for another purpose.