Docsity

Prolog Lists - Organization of Programming Languages - Lecture Notes, Study notes of Programming Languages

Partial preview of the text

Download Prolog Lists - Organization of Programming Languages - Lecture Notes and more Study notes Programming Languages in PDF only on Docsity!

Today ...

  • Project part 2
  • Reading and HW 9
  • Prolog lists

Variable naming and proof trees

Be careful with variable names

r(X, Y) :- r(X, Z), s(Z, Y).

solve [r(X,Y)]

solve [r(X,Z),s(Z,Y)]

solve [r(X,Z1),s(Z1,Z),s(Z,Y)]

We cannot use Z here again!!

• We cannot use Z here again!^ So we rename to Z1 …!

  • So we rename to Z ...

List predicates

Checking list membership

member(X, [X | ]). member(X, [ | T]) :- member(X, T).

Q: What is the proof tree for member(2, [1, 2])?

Q: What is the proof tree for member(2, [1, 3])?

Note this example works like pattern matching in Haskell ...

  • try first rule ... if it doesn’t succeed, try next candidate
  • unfold the rule
  • and so on

Q: How would we write a last predicate?

last(X, [X]). last(X, [_|T]) :- last(X, T).

Q: What is the proof tree for last(2, [1, 2])?