Lists Lists York University CSE 3401 Vida Movahedi 1 York - - PowerPoint PPT Presentation

lists lists
SMART_READER_LITE
LIVE PREVIEW

Lists Lists York University CSE 3401 Vida Movahedi 1 York - - PowerPoint PPT Presentation

Lists Lists York University CSE 3401 Vida Movahedi 1 York University CSE 3401 V. Movahedi 05_Lists Overview Overview Definition and representation of Lists in Prolog Definition and representation of Lists in Prolog Dot functor


slide-1
SLIDE 1

Lists Lists

York University CSE 3401 Vida Movahedi

York University‐ CSE 3401‐ V. Movahedi

1

05_Lists

slide-2
SLIDE 2

Overview Overview

  • Definition and representation of Lists in Prolog

Definition and representation of Lists in Prolog

– Dot functor

  • Examples of recursive definition of predicates

Examples of recursive definition of predicates

– islist, – member, delete d l i l – append, multiple, – prefix, suffix, sublist [ref.: Clocksin‐ Chap.3 and Nilsson‐ Chap. 7] [also Prof. Gunnar Gotshalks’ slides]

York University‐ CSE 3401‐ V. Movahedi

2

05_Lists

slide-3
SLIDE 3

Lists Lists

  • A list:

– is an ordered sequence of elements that can have any length. – It is a term. – Either an empty list [] or it has a head X and a tail L represented as [X|L] where X is a list item and L is a list. – List notation in Prolog: [a, b, c, d, ...]

  • The dot:
  • The dot:

– is a functor for representing lists with two arguments, the head and the tail of a list – A list of one element [a] is [a| [] ] implemented in Prolog as (a A list of one element [a] is [a| [] ] implemented in Prolog as .(a, []) – [a, b] is .(a, .(b, []))

  • Note [a, b, c] is not the same as [a, [b,c]]

York University‐ CSE 3401‐ V. Movahedi

3

05_Lists

slide-4
SLIDE 4

Lists (cont.) Lists (cont.)

[a b c] is (a (b (c []))) [a, b, c] is .(a, .(b, .(c, [])))

. a . b . b . c [] c []

York University‐ CSE 3401‐ V. Movahedi

4

05_Lists

slide-5
SLIDE 5

Examples Examples

  • Write the Prolog definition for being a list.

Write the Prolog definition for being a list.

islist([]). i li t([H d|T il]) i li t(T il) islist([Head|Tail]) :‐ islist(Tail).

W it th P l d fi iti f b i b f

  • Write the Prolog definition for being a member of a

list.

member(X, [X|L]). member(X, [Y|L]) :‐ member(X,L).

York University‐ CSE 3401‐ V. Movahedi

5

05_Lists

slide-6
SLIDE 6

Examples (cont.) Examples (cont.)

:‐ member(3, [2, 3, 4, 5]). true :‐ member(3, [2, [3, 4], 5]). f l

Our definition does not consider b f b ( d li )

false :‐ member(X, [1, 2]). X = 1 ;

members of members (nested lists) Unlike other programming

X = 1 ; X = 2 ; false

languages, inputs can be unknowns

:‐ member(2, L). L = [2|_] ; L=[_, 2 | _];

Note the recursive definition of member

...

York University‐ CSE 3401‐ V. Movahedi

6

05_Lists

slide-7
SLIDE 7

Recursive Search Recursive Search

  • Example:

Example:

member(X, [X|L]). : boundary condition member(X, [Y|L]) :‐ member(X,L). : recursive case ll bl a smaller problem :‐member(X, [a,b,c]). X = a; X = b; X = c; X c; false

York University‐ CSE 3401‐ V. Movahedi

7

05_Lists

slide-8
SLIDE 8

Delete Delete

  • delete(X, L1, L2) is true if L2 is the result of deleting X

delete(X, L1, L2) is true if L2 is the result of deleting X from L1 (just once).

– For example: delete(5, [1, 5, 4, 2], [1, 4, 2]). delete(X [X|L] L) delete(X, [X|L], L). delete(X, [Y|L], [Y|L1]) :‐ delete(X, L, L1).

York University‐ CSE 3401‐ V. Movahedi

8

05_Lists

slide-9
SLIDE 9

Append Append

  • Join two lists:

Example: append([1,2], [3,4], [1,2,3,4]) append([], L, L). : boundary condition append([X|L1], L2, [X|L3]) :‐ append(L1, L2, L3). : recursive case a smaller problem

  • Possible Queries:

[Nilsson] :‐ append([a, b], [c, d], [a, b, c, d]). true :‐ append([a, b], [c, d], X). X=[a, b, c, d]

  • r even

:‐ append(Y, Z, [a, b, c, d]).

York University‐ CSE 3401‐ V. Movahedi

9

05_Lists

slide-10
SLIDE 10

Search tree for append query Search tree for append query

append([], X, X). append([X|Y], Z, [X|W]) :‐ append(Y, Z, W).

:‐ append(Y, Z, [a, b, c, d]).

York University‐ CSE 3401‐ V. Movahedi

10

05_Lists

slide-11
SLIDE 11

Example: multiple occurrences in a list

  • multiple(L) is true if L is a list with multiple

Example: multiple occurrences in a list

multiple(L) is true if L is a list with multiple

  • ccurrences of some element [Nilsson]:

| multiple([Head|Tail]):‐ member(Head, Tail). multiple([Head|Tail]):‐ multiple(Tail). – Writing multiple(..) using append(..) multiple(L) :‐ append(L1, [X|L2], L), append(L3, [X|L4], L).

What is missing in definition of multiple(..)? How can it be corrected?

York University‐ CSE 3401‐ V. Movahedi

11

05_Lists

slide-12
SLIDE 12

Prefix/ Suffix with append Prefix/ Suffix with append

  • Write prefix(P,L) which is true if P is a prefix of L.

Write prefix(P,L) which is true if P is a prefix of L.

prefix(P, L):‐ append(P, _, L). – Is [] a prefix of L?

  • Write suffix(S,L) which is true if S is a suffix of L

( , )

suffix(S,L):‐ append(_, S, L).

  • Exercise: Try writing prefix and suffix without using append.

York University‐ CSE 3401‐ V. Movahedi

12

05_Lists

slide-13
SLIDE 13

More Examples with append More Examples with append

  • sublist(S,L) is true if S is a sublist of L

sublist(S,L) is true if S is a sublist of L

– in other words, S is the suffix of a prefix – Using append(..): sublist(S,L):‐ append(_, S, Left), append(Left, _, L).

York University‐ CSE 3401‐ V. Movahedi

13

05_Lists

slide-14
SLIDE 14

More Examples with append More Examples with append

  • Re‐writing delete(X,L1,L2) with append(..):

Re writing delete(X,L1,L2) with append(..):

delete(X, L, R):‐ append(L1,[X|L2],L), append(L1, L2, R).

York University‐ CSE 3401‐ V. Movahedi

14

05_Lists

slide-15
SLIDE 15

Append is expensive! Append is expensive!

append([], L, L). pp ([], , ) append([X|L1], L2, [X|L3]) :‐ append(L1, L2, L3).

  • The complexity of appending two lists, L1 and L2, is

The complexity of appending two lists, L1 and L2, is O(n) where n is the length of the first list.

  • Consider reverse(L R) defined as:
  • Consider reverse(L, R) defined as:

reverse([], []). reverse([X|L], R) :‐ reverse(L, L1), append (L1, [X], R).

  • Complexity of reverse(..) is O(n2) where n is the

length of L. g

York University‐ CSE 3401‐ V. Movahedi

15

05_Lists