PROLOG Exercises

Author: Jack L. Watkin

CPS 499-03: Emerging Languages, Spring 2017

  1. A multiplexer is a device that selects one of many inputs to output based on a select line input. Define a PROLOG mux/7 predicate that acts as a 4-input, 2-bit multiplexer.

    Examples:
    ?> mux("1","2","3","4",1,1,Output).
    Output = "4".
    
    ?> mux("1","2","3","4",0,1,Output).
    Output = "2".
    
  2. Define a PROLOG predicate route/3 that takes two cities and a route and returns whether that route is a valid route. Excluding fact declarations, this program requires approximately 15 lines of code. The roads need not be implicitly bi-directional.

    Sample list of cities:
    road(paris,rouen).
    road(paris,lyon).
    road(lyon,marseille).
    road(marseille,nice).
    road(paris,bordeaux).
    road(paris,caen).
    road(bordeaux,madrid).
    road(madrid,cuenca).
    
    Examples:
    ?> route(paris,caen,[paris, caen]).
    true.
    
    ?> route(paris,cuenca,Route).
    Route = [paris, bordeaux, madrid, cuenca].
    
  3. Define a PROLOG predicate expr that takes an infix numerical expression and the result and returns whether the given result is the correct result. The predicate need not handle a divide by 0 error. Use the following grammar:
    (r1)<expr>::= <number> <op> <expr>
    (r2)<expr>::= <number> <op> <number>
    (r3)<op>::= + | - | * | /

    Examples:
    ?> expr([3,*,39,+,3], 120).
    true.
    
    ?> expr([3,*,39,+,3], 39).
    false.
    
    ?> expr([3,*,39,+,3], X).
    X = 120.