Prolog Programming (Volume 3)

Slides:



Advertisements
Similar presentations
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.
Advertisements

Declarative Programming Lists in PROLOG Autumn 2014.
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 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.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Logic Programming Lecture 3: Recursion, lists, and data structures.
Data Compressor---Huffman Encoding and Decoding. Huffman Encoding Compression Typically, in files and messages, Each character requires 1 byte or 8 bits.
Chapter 8 . Sequence Control
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.
CS10 Final Review by Glenn Sugden is licensed under a Creative Commons Attribution-NonCommercial-ShareAlike 3.0 Unported License.Glenn Sugdenmons Attribution-NonCommercial-ShareAlike.
Lists in Python.
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.
CE Operating Systems Lecture 17 File systems – interface and implementation.
Hashing Fundamental Data Structures and Algorithms Margaret Reid-Miller 18 January 2005.
Types of Algorithms. 2 Algorithm classification Algorithms that use a similar problem-solving approach can be grouped together We’ll talk about a classification.
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.
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.
Data Structures for Disjoint Sets Manolis Koubarakis Data Structures and Programming Techniques 1.
Recursion ● Recursion or Iteration ● A Common Recursive Design Pattern ● Computing factorials ● Searching a filesystem tree ● Faster exponentiation ● Slow.
COSC 2P93 Prolog: Debugging
May 3rd – Hashing & Graphs
Graphs: Definitions and Basic Properties
CHP - 9 File Structures.
Top 50 Data Structures Interview Questions
Containers and Lists CIS 40 – Introduction to Programming in Python
Prolog: Control, Negation and Cut
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.
Efficiency add remove find unsorted array O(1) O(n) sorted array
IGCSEFM :: Domain/Range
Types of Algorithms.
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.
Types of Algorithms.
Merge Sort 11/28/2018 2:21 AM The Greedy Method The Greedy Method.
Prolog Lists.
Prolog Programming (Volume 2)
C Graphing Functions.
Basic Counting.
EA C461 – Artificial Intelligence Problem Solving Agents
CH 9.2 : Hash Tables Acknowledgement: These slides are adapted from slides provided with Data Structures and Algorithms in C++, Goodrich, Tamassia and.
FUNCTION NOTATION AND EVALUATING FUNCTIONS
Programming Paradigms and Languages
Prolog Lists.
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.
Do Now: Make a K-W-L Chart Complete what you KNOW about functions
Programming Techniques
File Compression Even though disks have gotten bigger, we are still running short on disk space A common technique is to compress files so that they take.
General External Merge Sort
Types of Algorithms.
Prolog Programming (Volume 4)
UNDERSTANDING FUNCTIONS
CS222/CS122C: Principles of Data Management UCI, Fall 2018 Notes #05 Index Overview and ISAM Tree Index Instructor: Chen Li.
Heaps.
Lecture-Hashing.
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([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].

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