Announcements § Quiz 05 and Homework 06 open today and are due December 1. All about Prolog § December 1. Final Review. Feel free to share questions § December 3. Final Exam during the lecture time. Comprehensive Exam
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
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.
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
Spring 2018 Disclaimer. These slides can only be used as study material for the class CSE240 at ASU. They cannot be distributed or used for another purpose.