Recursive case abcd bcd thirdlastabcdans

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: view •  Exercise 1 (4pts): [a,b,c,d] =[Head|Tail] rd –  write a recursive defini=on ] = Tail [b,c,d thirdlast/2 to find the 3 last element of a list thirdlast([Head|Tail],Ans) :thirdlast(Tail,Ans). •  Recursive case –  [a,b,c,d] –  [b,c,d] thirdlast([a,b,c,d],Ans) Base case thirdlast([b,c,d],Ans) thirdlast([b,c,d],d). thirdlast([X,_,_],X). –  [a,b,c,d,e]thirdlast([a,b,c,d],Ans) –  [b,c,d,e] thirdlast([b,c,d],Ans) Base case thirdlast([c,d,e],c). thirdlast([c,d],Ans) –  [c,d,e] thirdlast([X,_,_],X). Homework 2 Review •  Exercise 1 (4pts): –  write a recursive defini=on thirdlast/2 to find the 3rd last element of a list –  e.g. ?- thirdlast([a,b,c,d,e],X). X=c •  Another method, use append/3 in list ­spliWng mode –  Key insight: you can make the 2nd list exactly 3 elements long –  thirdlast(L,X) :- append(_,[X,_,_],L). Homework 2 Review •  Exercise 2 (6pts): –...
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