More examples - PowerPoint PPT Presentation

1 / 8
About This Presentation
Title:

More examples

Description:

In the original program, the relation arrow(X,Y) represents a directed link. ... E.g. tube line is [bakerloo, central], and stations are ... – PowerPoint PPT presentation

Number of Views:45
Avg rating:3.0/5.0
Slides: 9
Provided by: rong94
Category:

less

Transcript and Presenter's Notes

Title: More examples


1
More examples
  • Todays examples are related to coursework

2
Once upon a time, an insect is crawling along a
wire cube,
  • In the original program, the relation arrow(X,Y)
    represents a directed link. How do we extend it
    to a bi-directional link?
  • Easy using disjunction
  • connected(X, Y)- arrow(X, Y).
  • connected(X, Y)- arrow(Y, X).

3
Re-look at cango program
  • Can we simply replace arrow by connected?
  • cango(X,Y)- connected(X,Y).
  • cango(X,Y)- connected(X,Z), cango(Z,Y).
  • No. There are circles in the map there is a
    danger of being trapped in a circle. Thus, we
    must add something to avoid this.

4
The new cango program with circle detection
  • Need an extra list to store
  • the path generated so far
  • cango(X,Y, CurrentPath)-
  • connected(X,Y),
  • \ member(Y,CurrentPath).
  • cango(X,Y, CurrentPath)-
  • connected(X,Z),
  • \ member(Z,CurrentPath),
  • cango(Z,Y, ZCurrentPath).
  • (\ means not)
  • If we want to return a path,
  • then add the 4th argument
  • cango(X,Y, P, FinalPath)-
  • connected(X,Y),
  • \ member(Y,P),
  • FinalPath YP.
  • cango(X,Y, CP, FP)-
  • connected(X,Z),
  • \ member(Z,CP),
  • cango(Z,Y, ZCP, FP).

5
When call cango, we must initialize the 3rd
argument Question What is the CurrentPath
initially?
  • Answer it is the start point.
  • ?- cango(a,h, a, Path).
  • Path h,d,b,a,
  • Why the returned Path is reversed?
  • In each recursion, the 3rd argument is added an
    element
  • at its front a, b,a, d,b,a, and finally
    h,d,b,a
  • This kind of list is called an accumulator

6
How to reverse a list another example of using
accumulator
  • Naïve reverse
  • rev1(HT, R)-
  • rev1(T, RT),
  • append(RT,H, R).
  • append(, L, L).
  • append(HT,X,HL)-
  • append(T,X,L).
  • More efficient reverse the
  • 3rd arg. is an accumulator
  • rev2(L, RL)- init accumulator
  • rev2(L, RL, ).
  • rev2(HT, RL, CL)-
  • rev2(T, RL, HCL).
  • rev2(, RL, CL)- base
  • RLCL.

7
Formatting output
  • Assume a route is represented by a list of
    stations and a list of tube lines,
  • How to print it out? E.g. tube line is bakerloo,
    central, and stations are
  • baker_street, regents_park, oxford_circus,
    tottenhan_court_road, holborn
  • a simple solution (you can improve it)
  • myprint(_, _) - nl.
  • myprint(X,YT1,LT2)-
  • connected(X,Y,L),
  • write(Take ), write(L), write( line),
  • write( from ), write(X), write( to ),
    write(X), nl,
  • myprint(YT1, LT2).
  • myprint(X,YT1,LT2)-
  • \ connected(X,Y,L), need to change to
    another line
  • myprint(X,YT1, T2).

8
Implementing a user interface
  • An example from last lecture - repeat
    promptreaddo
  • sq- write(Give a number ), read(X),
    sq_loop(X).
  • sq_loop(X)- Xexit, write(bye), nl. stop
  • sq_loop(X)- \ number(X),
  • write(input error), nl, sq. restart, ask
    for another
  • sq_loop(X)- number(X),
  • C is XX, write(sqr(X)), write( ), write(C),
    nl,
  • sq. carry on getting input
Write a Comment
User Comments (0)
About PowerShow.com