CSC302 2011S, Class 10: Prolog (3) Overview: * Add to End example * Thinking in Prolog. * Lab. Admin: * Late or missing: JL, AT * Are there questions on Assignment 4? * EC for Thursday extra: Our own Aaron Todd! * EC for Friday CS Table: Knuth on the History of Combinatorial Generation. * Due to popular demand, we will do a fourth day of Prolog on Wednesday, completely screwing up my schedule. * There is no reading for Tuesday night. Problem: Write add_to_end(L, X, M), which holds when M is the result of adding X to the end of L. (Alternately: Which adds X to the end of L.) add_to_end([], X, [X]). add_to_end([H|T], X, M) :- add_to_end(T, X, U), M=[H|U]. vs. add_to_end([], X, [X]). add_to_end([H|T], X, [H|U]) :- add_to_end(T, X, U). -- Thinking in Prolog * 1: Try to think in terms of patterns. * 2. Worry about the evaluation strategy (agh!) * 3: Worry about multiple paths to the same answer. * Add explicit rules to ensure that, for example, V
P * Side note: =< not <= 4. Conflict between two views: This predicate holds when ... Given X, this predicate computes Y.q Example: Length (See Examples/Day3) -- Lab For printing the puzzle: Helpful to write: (a) split(L, N, L1, L2), which holds when L1 contains the first N characters of L and L2 contains the remaining characters. (b) print_list(L), which prints a list separated by spaces