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

CSE240 Lecture 25

CSE240 Lecture 25

Introduction to Programming Languages
Expressions with Prolog
(202112)

Javier Gonzalez-Sanchez

January 25, 2017
Tweet

More Decks by Javier Gonzalez-Sanchez

Other Decks in Programming

Transcript

  1. 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

    View full-size slide

  2. jgs
    Expressions

    View full-size slide

  3. 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

    View full-size slide

  4. 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

    View full-size slide

  5. 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

    View full-size slide

  6. 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

    View full-size slide

  7. jgs
    Rules and Recursion

    View full-size slide

  8. 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).

    View full-size slide

  9. 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.

    View full-size slide

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

    View full-size slide

  11. 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.

    View full-size slide

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

    View full-size slide

  13. jgs
    Final Exam Review

    View full-size slide

  14. 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

    View full-size slide

  15. 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

    View full-size slide

  16. jgs
    Test Yourselves
    C/C++

    View full-size slide

  17. 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);
    }

    View full-size slide

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

    View full-size slide

  19. jgs
    Test Yourselves
    Lisp

    View full-size slide

  20. 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)) )
    )

    View full-size slide

  21. 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

    View full-size slide

  22. 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)))

    View full-size slide

  23. 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) )

    View full-size slide

  24. 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)

    View full-size slide

  25. jgs
    Test Yourselves
    Prolog

    View full-size slide

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

    View full-size slide

  27. 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).

    View full-size slide

  28. 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

    View full-size slide

  29. 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).

    View full-size slide

  30. 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).

    View full-size slide

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

    View full-size slide

  32. 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

    View full-size slide

  33. 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.

    View full-size slide