Todays topics homework 2 review reverse3 from last me

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: allbutlast(L1,L2) is true if list L2 is L1 minus the last element •  not defined •  [a] •  [a,b] [a] •  [a,b,c] [a,b] Recursive case [a,b,c] = [Head|Tail] [a,b] = [Head|Tail’] allbutlast([Head|Tail],[Head|Tailp]) :- allbutlast(Tail,Tailp). 13 Homework 2 Review •  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 allbutlast([_],). allbutlast([H|T],[H|Tp]) :- allbutlast(T,Tp). midlist([X],X). midlist([Head|T],X) :- append(Lp,[Last],T), midlist(Lp,X). append(Lp[Last],T)= allbutlast(T,Lp) midlist([X],X). midlist([Head|T],X) :- allbutlast(T,Lp), midlist(Lp,X). 14 Homework 2 Review •  another solu=on –  … equivalent to cu/ng a deck of cards in half … –  Key insight: append/3 can give all possible ways of spliWng a list –  we just need to make sure the deck is precisel...
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 University of Arizona- Tucson.

Ask a homework question - tutors are online