Prolog Programming (Volume 3) Dr W.F. Clocksin. Mapping: The Full Map sqlist(, ) sqlist([], []). sqlist([X|T], [Y|L]) :- Y is X * X, sqlist(T, L). List.

Slides:



Advertisements
Similar presentations
Information Retrieval in Practice
Advertisements

Prolog Programming (Volume 2) Dr W.F. Clocksin. Lists Lists are the same as other languages (such as ML) in that a list of terms of any length is composed.
Intro to Scala Lists. Scala Lists are always immutable. This means that a list in Scala, once created, will remain the same.
Hash Tables CS 310 – Professor Roch Weiss Chapter 20 All figures marked with a chapter and section number are copyrighted © 2006 by Pearson Addison-Wesley.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
Declarative Programming Lists in PROLOG Autumn 2014.
Jeffrey D. Ullman Stanford University. 2  A set of nodes N and edges E is a region if: 1.There is a header h in N that dominates all nodes in N. 2.If.
Prolog Programming (Volume 5) Dr W.F. Clocksin. List cells + Unification = Great Idea Normally we use ‘proper lists’, which are defined according to the.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
Prolog: List © Patrick Blackburn, Johan Bos & Kristina Striegnitz.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
Logic Programming Lecture 3: Recursion, lists, and data structures.
Graphs Graphs are the most general data structures we will study in this course. A graph is a more general version of connected nodes than the tree. Both.
Data Compressor---Huffman Encoding and Decoding. Huffman Encoding Compression Typically, in files and messages, Each character requires 1 byte or 8 bits.
Amirkabir University of Technology Computer Engineering Faculty AILAB Parsing Ahmad Abdollahzadeh Barfouroush Aban 1381 Natural Language Processing Course,
This material in not in your text (except as exercises) Sequence Comparisons –Problems in molecular biology involve finding the minimum number of edit.
Chapter 8 . Sequence Control
Clicker Question 1 Are you (and your clicker) here? A. Yes B. No C. Not sure. Too early.
Backtracking Reading Material: Chapter 13, Sections 1, 2, 4, and 5.
Chapter 9 Introduction to Arrays
Function: Definition A function is a correspondence from a first set, called the domain, to a second set, called the range, such that each element in the.
Exercise problems for students taking the Programming Parallel Computers course. Janusz Kowalik Piotr Arlukowicz Tadeusz Puzniakowski Informatics Institute.
CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike.
Slides for “Data Mining” by I. H. Witten and E. Frank.
Lists in Python.
©Ian Sommerville 2000 Software Engineering, 6th edition. Chapter 20 Slide 1 Defect testing l Testing programs to establish the presence of system defects.
CSC 480 Software Engineering Lecture 14 Oct 16, 2002.
CHAPTER 09 Compiled by: Dr. Mohammad Omar Alhawarat Sorting & Searching.
March 16 & 21, Csci 2111: Data and File Structures Week 9, Lectures 1 & 2 Indexed Sequential File Access and Prefix B+ Trees.
30/09/04 AIPP Lecture 3: Recursion, Structures, and Lists1 Recursion, Structures, and Lists Artificial Intelligence Programming in Prolog Lecturer: Tim.
Hey, Ferb, I know what we’re gonna do today! Aims: Use formatted printing. Use the “while” loop. Understand functions. Objectives: All: Understand and.
Lecture 7 Introduction to Programming in C Arne Kutzner Hanyang University / Seoul Korea.
The Loop Macro Many of the CL “functions” are actually macros (let, progn, if, etc) The most complicated macro in CL is probably the Loop macro –The Loop.
Hashing Sections 10.2 – 10.3 CS 302 Dr. George Bebis.
N-space Snakes are special maximal length loops through an N-space cube. They ’ re full of intriguing symmetries, puzzles and surprises. They ’ re simple.
Ch. 13 Ch. 131 jcmt CSE 3302 Programming Languages CSE3302 Programming Languages (notes?) Dr. Carter Tiernan.
CE Operating Systems Lecture 17 File systems – interface and implementation.
Hashing Fundamental Data Structures and Algorithms Margaret Reid-Miller 18 January 2005.
CS4432: Database Systems II Query Processing- Part 2.
Types of Algorithms. 2 Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together We’ll talk about a classification.
TM Design Macro Language D and SD MA/CSSE 474 Theory of Computation.
1 Directed Graphs Chapter 8. 2 Objectives You will be able to: Say what a directed graph is. Describe two ways to represent a directed graph: Adjacency.
Simple Data structure and computation. Solving equation? sum(X,Y,Z):- Z is X+Y. ?sum(1,R,7). is/2: Arguments are not sufficiently instantiated ^ Exception:
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
Searching ( 搜索) 1. Introduction 2. Sequential Search 3. Binary Search 4. Comparison Trees.
Prolog 3 Tests and Backtracking 1. Arithmetic Operators Operators for arithmetic and value comparisons are built-in to Prolog = always accessible / don’t.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Copyright © 2014 Curt Hill Algorithms From the Mathematical Perspective.
Graphs Definition: a graph is an abstract representation of a set of objects where some pairs of the objects are connected by links. The interconnected.
Functional Processing of Collections (Advanced) 6.0.
Section Recursion 2  Recursion – defining an object (or function, algorithm, etc.) in terms of itself.  Recursion can be used to define sequences.
Top 50 Data Structures Interview Questions
Courtsey & Copyright: DESIGN AND ANALYSIS OF ALGORITHMS Courtsey & Copyright:
Review Graph Directed Graph Undirected Graph Sub-Graph
Hashing Exercises.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
Recursive Thinking Chapter 9 introduces the technique of recursive programming. As you have seen, recursive programming involves spotting smaller occurrences.
CS222P: Principles of Data Management Notes #6 Index Overview and ISAM Tree Index Instructor: Chen Li.
Prolog Programming (Volume 3)
Prolog Programming (Volume 2)
C Graphing Functions.
One-Pass Algorithms for Database Operations (15.2)
Hashing Sections 10.2 – 10.3 Lecture 26 CS302 Data Structures
Fundamentals of Functional Programming
CS222/CS122C: Principles of Data Management Notes #6 Index Overview and ISAM Tree Index Instructor: Chen Li.
Programming Techniques
FUNCTIONS.
General External Merge Sort
CS222/CS122C: Principles of Data Management UCI, Fall 2018 Notes #05 Index Overview and ISAM Tree Index Instructor: Chen Li.
Presentation transcript:

Prolog Programming (Volume 3) Dr W.F. Clocksin

Mapping: The Full Map sqlist(, ) sqlist([], []). sqlist([X|T], [Y|L]) :- Y is X * X, sqlist(T, L). List of numbers List of squares of numbers

Mapping: The Full Map (cont’d) Here is an exercise in compound terms. Map each list element (a number) to a term s(A,B) where A is the number and B is its square. Input: [1, 9, 15] Output: [s(1,1), s(9, 81), s(15, 225)] sqterm([], []). sqterm([X|T], [s(X,Y)|L]) :- Y is X * X, sqterm(T, L).

General Scheme for Full Map /* fullmap(In, Out) */ fullmap([], []). fullmap([X|T], [Y|L]) :- transform(X,Y), fullmap(T, L). Here is a typical transformation table…

Simple Transformation transform(cat, gatto). transform(dog, cane). transform(hamster, criceto). transform(X, X). ?- fullmap([cat, dog, goat], Z). Z = [gatto, cane, goat] A ‘catchall’ rule

Worksheet 11: Multiple Choices Sometimes the map needs to be sensitive to the input data: Input: [1, 3, w, 5, goat] Output: [1, 9, w, 25, goat] squint([], []). squint([X|T], [Y|L]) :- integer(X), Y is X * X, squint(T, L). squint([X|T], [X|L]) :- squint(T,L). Just use a separate clause for each choice

Multiple Choices Using the infix binary compound term *, it is easy enough to give some mathematical reality to the map: Input: [1, 3, w, 5, goat] Output: [1, 9, w*w, 25, goat*goat] squint([], []). squint([X|T], [Y|L]) :- integer(X), Y is X * X, squint(T, L). squint([X|T], [X*X|L]) :- squint(T,L).

Worksheet 12: Partial Maps Given an input list, partially map it to an output list. evens([], []). evens([X|T], [X|L]) :- 0 is X mod 2, evens(T, L) evens([X|T], L) :- 1 is X mod 2, evens(T, L). ?- evens([1, 2, 3, 4, 5, 6], Q), Q = [2, 4, 6].

General Scheme for Partial Maps partial([], []). partial([X|T], [X|L]) :- include(X), partial(T, L) partial([X|T], L) :- partial(T, L). For example, include(X) :- X >= 0. ?- partial([-1, 0, 1, -2, 2], X), X = [0, 1, 2].

Partial Maps Exercise: Write a program that ‘censors’ and input list, by making a new list in which certain prohibited words do not appear. To do this, define a predicate prohibit such that prohibit(X) succeeds if X is a censored word. For example, prohibit(bother). prohibit(blast). prohibit(drat). prohibit(fiddlesticks).

Worked example censor([], []). censor([H|T], T1) :- prohibit(H), censor(T, T1). censor([H|T], [H|T1]) :- censor(T, T1).

Worksheet 13: Removing Duplicates setify([], []). setify([X|T], L) :- member(X,T), setify(T, L). setify([X|T], [X|L]) :- setify(T, L).

WS14: Partial Maps with a Parameter Before, we saw how to prevent loops (when searching a graph) by keeping a ‘trail’ of the nodes visited so far. Here is another way to think about the problem. Keep a list of all the visitable nodes. As each node is visited, strike it off the list and pass the reduced list to the recursive call. Backtracking restores the old list, so alternative paths can be searched.

WS14: Partial Maps with a Parameter First, a predicate to reduce the list. Goal reduce(L,X,M) succeeds for input list L, term X and output list M, when M contains the elements of L except for the first occurrence of X. Thus, X is a parameter that controls which element will be omitted from the output list. reduce([X|T], X, T). reduce([H|T], X, [H|L]) :- reduce(T, X, L).

Use this for searching as follows path(X, X, L). path(X, Y, L) :- a(X, Z), reduce(L, Z, L1), path(Z, Y, L1). Using the arc relation on Worksheet 9, ?- path(a, b, [a, b, c, d, e, f, g, h]). yes.

WS 15: Multiple Disjoint Partial Maps Maps a list into several disjoint lists. Separating sheep from goats. Define the predicate herd, such that the goal herd(L, S, G) succeeds if S is a list of all the sheep in L and G is a list of all the goats in L. herd([], [], []). herd([sheep|T], [sheep|S], G) :- herd(T, S, G). herd([goat|T], S, [goat|G]) :- herd(T, S, G).

What do the following goals do? ?- herd([sheep, goat, goat, sheep, goat], X, Y). ?- herd([goat, sheep, stone, goat, tree], X, Y). ?- herd(X, [sheep, sheep], [goat, goat]).

How to deal with other objects? ?- herd([goat, sheep, stone, goat, tree], X, Y). The above goal fails. Instead, we could ignore other objects by having a catchall: herd([X|T], S, G) :- herd(T, S, G).

Or, collect them in a list also input sheep goats extras herd([], [], [], []). herd([sheep|T], [sheep|S], G, E) :- herd(T, S, G, E). herd([goat|T], S, [goat|G], E) :- herd(T, S, G, E). herd([X|T], S, G, [X|E]) :- herd(T, S, G, E).

Example: Alternating a list Maps an input list into a pair of lists, alternating the elements. alternate([a, b, c, d, e, f], [a, c, e], [b, d, f]) Alternating by index (which element of the input list it is): altx([], [], []). altx([A, B | T], [A | T1], [B| T2]) :- altx(T, T1, T2).

Or, alternating by value Maps an input list into a pair of lists, alternating the elements. altv([1, 2, 3, 4], [2, 4], [1, 3]) altv([], [], []). altv([A | T], [A | T1], B) :- 0 is A mod 2, altv(T, T1, B). altv([B | T], A, [B | T2]) :- 1 is B mod 2, altv(T, A, T2). See how this is the same as sheep and goats?

WS 17: Full maps with state /* mapsum(list of integers, cumulative sum) */ ?- mapsum([1, 3, 2, 5, 4], X). X = [1, 4, 6, 11, 15]. 0

mapsum Use an accumulator as a state variable that helps to determine the value of each element of the output list. /* ms(input list, accumulator, output list) */ mapsum(A, B) :- ms(A, 0, B) ms([], _, []). ms([H|T], N, [C|L]) :- C is H + N, ms(T, C, L). initialise accumulator anonymous variable (don’t care about accumulator if end of list)

WS 18: Sequential Partial Map with State Example: Run length encoding is a useful data representation and compression technique. Represent sequential ‘runs’ of N identical terms T as the term N*T, for example [12, 2, 2, w, 3, 3, s, s, s] maps to [1*12, 2 * 2, 1 * w, 2 * 3, 3 * s].

WS 18: Sequential Partial Map with State [12, 2, 2, w, 3, 3, s, s, s] [1*12, 2 * 2, 1 * w, 2 * 3, 3 * s] runcode(input, current term, current count, output) runcode([], C, N, [N*C]). runcode([H|T], H, N, Z) :- N1 is N+1, runcode(T, H, N1, Z). runcode([H|T], C, N, [N*C|Z]) :- H \== C, runcode(T, H, 1, Z).

Sequential Partial Map with State runcode([], C, N, [N*C]). runcode([H|T], H, N, Z) :- N1 is N+1, runcode(T, H, N1, Z). runcode([H|T], C, N, [N*C|Z]) :- H \== C, runcode(T, H, 1, Z). End of list? Deal with leftover accumulator values Have seen this before... Otherwise, discharge the current run......and start a new run