Allbutlast ab a abc ab homework 2 review

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

View Full Document Right Arrow Icon
This is the end of the preview. Sign up to access the rest of the document.

Unformatted text preview: rue midlist([Head|Tail],X) : ­ append(Listp,[Last],Tail), midlist(Listp,X). Homework 2 Review •  Exercise 2 (6pts): –  Write a recursive defini=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] midlist([X],X). midlist([Head|T],X) :append(Lp,[Last],T), midlist(Lp,X). Homework 2 Review •  Solu=on using append/3: midlist([X],X). midlist([Head|T],X) :append(Lp,[Last],T), midlist(Lp,X). •  Solu=on using 2nd recursion: –  write a predicate allbutlast/2 in place of append/3 such that •  allbutlast(L1,L2) is true if list L2 is L1 minus the last element •  not defined Base case •  [a] allbutlast([a],). allbutlast([_],). •  [a,b] [a] •  [a,b,c] [a,b] Homework 2 Review •  Solu=on using 2nd recursion: –  write a predicate allbutlast/2 in place of append/3 such that •...
View Full Document

This note was uploaded on 04/29/2012 for the course LING 388 taught by Professor Staff during the Fall '08 term at Arizona.

Ask a homework question - tutors are online