Upgrade to Pro — share decks privately, control downloads, hide ads and more …

CSE240 Lecture 27

CSE240 Lecture 27

Introduction to Programming Languages
Recursion in Prolog
(201701)

B546a9b97d993392e4b22b74b99b91fe?s=128

Javier Gonzalez
PRO

January 27, 2017
Tweet

Transcript

  1. jgs CSE 240 Introduction to Programming Languages Lecture 27: Recursion

    in Prolog Javier Gonzalez-Sanchez javiergs@asu.edu PERALTA 230U Office Hours: By appointment
  2. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 2 jgs

    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
  3. jgs Previously …

  4. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 4 jgs

    A program is Facts, Rules … % facts instructor(john,cs365). instructor(mary,cs311). instructor(paul,cs446). enrolled(joseph,cs311). enrolled(joseph,cs365). enrolled(joseph,cs446). enrolled(danielle,cs365). enrolled(danielle,cs446). % rules teaches(P,S) :- instructor(P,C), enrolled(S,C). classmates(S1, S2) :- enrolled(S1, C), enrolled(S2, C).
  5. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 5 jgs

    … and Queries (part 1 / 2) ?- enrolled (joseph, cs365). true ?- enrolled (X, cs365). X=joseph X=danielle ?- teaches (X, joseph). X=john X=mary X=paul ?- classmates (joseph, danielle). true ?- classmates (joseph, jessica). false ?- classmates (jessica, danielle). false
  6. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 6 jgs

    … and Queries (part 2 / 2) % An additional rule classmates (S1, S2, C) :- enrolled (S1, C), enrolled (S2, C) ?- classmates (joseph, danielle, C). cs365 cs446 ?- classmates (joseph, jessica, C). false ?- classmates (jessica, danielle, C). false
  7. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 7 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
  8. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 8 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
  9. jgs Rules and Recursion

  10. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 10 jgs

    Rules as functions § doSomething(A,B,P) :- P is A + B. ?-doSomething(3,2,P). ?-doSomething(3,2,10).
  11. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 11 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.
  12. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 12 jgs

    Recursion | Factorial
  13. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 13 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.
  14. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 14 jgs

    Recursion | Fibonacci
  15. Javier Gonzalez-Sanchez | CSE240 | Spring 2018 | 15 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
  16. jgs CSE 240 Introduction to Programming Languages Javier Gonzalez-Sanchez javiergs@asu.edu

    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.