CSC302 2011S, Class 09: Prolog (2) Overview: * Math in Prolog. * Lists in Prolog. * Concatenating Lists. * Lab. Admin: * Cocoa * Missing: SA, AC * HW4 to be posted some time tonight. * I had started to design one assignment, but discarded it after spending too much time trying to solve it myself. * EC for CS Table today. Reading: Wikipedia on Nearest Neighbor. Day One: * Prolog is a DECLARATIVE programming language with emphasis on predicate logic. * Syntax. "It's not a real programming language unless you can do math" * You can do math with predicate logic add(zero,X,X). add(s(X),Y,Z) * For efficiency and uniformity and accuracy, build it in thing is expression: A goal or claim * Evaluation: Evaluate the expression as you would evaluate any mathematical expression If the thing on the left is a number, compare and return the result of the comparison If the thing on the left is a variable, assign and return true "It's not a real programming language unless you have data structures" Lists * Syntactic sugar makes life more fun (or more confusing) * empty list: [] * cons [H|T] * Get car and cdr car([H|_], H). cdr([_|T], T). * More sugar [a,b,c] [a,b|T]$a Tate's problem: How do we concatenate two lists together? concat(L1,L2,L3) - L3 is the result of concatenating L1 and L2. concat([], L, L). concat([H|T], L2, L3) :- concat(T,L2,Int), L3 = [H|Int]. concatenate([], L, L). concatenate([H|T1], L2, [H|T3]) :- concatenate(T1, L2, T3). Lab! For add_to_end problem, you base case should probably be add_to_end([], V, [V]).