lecture7

# G midlistabcdex xc midlistabcdx no

This preview shows page 1. Sign up to view the full content.

This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: pick out the middle element of a list –  (if there is no middle element, it should fail to return an answer) –  e.g. ?- midlist([a,b,c,d,e],X). X=c –  ?- midlist([a,b,c,d],X). No •  Possible lists –  –  –  –  –  –  [a] [a,b] [a,b,c] [a,b,c,d] [a,b,c,d,e] List Key observa.on: recursive case such that concatena=ng List’ with Last = Tail List = [Head|Tail] i.e. append(List’,[Last],Tail) is true Head List’ Last a List’ e Homework 2 Review •  Exercise 2 (6pts): –  Write a recursive deﬁni=on midlist/2 to pick out the middle element of a list –  (if there is no middle element, it should fail to return an answer) –  e.g. ?- midlist([a,b,c,d,e],X). X=c –  ?- midlist([a,b,c,d],X). No •  Possible lists –  –  –  –  –  –  [a] [a,b] [a,b,c] [a,b,c,d] [a,b,c,d,e] List = [Head|Tail] Head List’ Last append(List’,[Last],Tail) is t...
View Full Document

{[ snackBarMessage ]}

Ask a homework question - tutors are online