324-lecture6

# 324-lecture6 - Principles Principles of Programming...

This preview shows pages 1–7. Sign up to view the full content.

Principles of Programming Languages Lecture 6 Wael Aboulsaadat wael@cs.toronto.edu http://portal.utoronto.ca/ 1

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Prolog: recursion Prolog: recursion – examples examples Appending lists: – Declarative Semantics: Appending an empty list to a non-empty list is the non-empty list else work on one list by removing its elements and adding it to the other list. – Prolog pend([],L,L). append([],L,L). append([H | L1], L2, [H | L3]) :- append(L1,L2,L3). |?- append([a,b,c],[1,2,3], R). R= [a,b,c,1,2,3] 2
Prolog: recursion Prolog: recursion – examples examples Appending lists: append([],L,L). append([H | L1], L2, [H | L3]) :- append(L1,L2,L3). ?- append([a,b,c],[1,2,3], R). / \ R = [a|L0] ?- append([b,c],[1,2,3],L0) / \ L0=[b|L1] ?- append([c],[1,2,3],L1) / \ L1=[c|L2] ?- append([],[1,2,3],L2) / \ L2=[1,2,3] L2=[1,2,3] L1=[c|L2]=[c,1,2,3] L0=[b|L1]=[b,c,1,2,3] R=[a|L0]=[a,b,c,1,2,3] 3

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Prolog: recursion Prolog: recursion – examples examples Blocks: – Declarative Semantics: Block X is above block Y if X is placed on top of Y, or X is placed on top of some block Z that is above Y. – Prolog: a b c d above(X,Y) :- on(X,Y). (1) above(X,Z) :- above(X,Y), above(Y,Z). (2) on(a,b). (3) on(b,c). (4) on(c,d). (5) |?- above(a,b). Yes |? above(a,d). Yes |? above(c,a). Infinite recursion! above(a,b) b on(a,b) above(a,d) b above (a,Y) b above(a,b) b above(b,d) b above(b,Y) b above(b,c) b above(c,d) b true 4
Prolog: recursion Prolog: recursion – examples examples Blocks: – Declarative Semantics: Block X is above block Y if X is placed on top of Y, or X is placed on top of some block Z that is above Y. – Prolog: a b c d % Second attempt above(X,Y) :- on(X,Y). (1) above(X,Z) :- on(X,Y), above(Y,Z). (2) on(a,b). (3) on(b,c). (4) on(c,d). (5) |?- above(a,d). Yes 5

This preview has intentionally blurred sections. Sign up to view the full version.

View Full Document
Prolog: recursion Prolog: recursion – examples examples Blocks: – Declarative Semantics: Block X is above block Y if X is placed on top of Y, or X is placed on top of some block Z that is above Y.
This is the end of the preview. Sign up to access the rest of the document.

## This document was uploaded on 08/02/2011.

### Page1 / 17

324-lecture6 - Principles Principles of Programming...

This preview shows document pages 1 - 7. Sign up to view the full document.

View Full Document
Ask a homework question - tutors are online