Utility programs In utilities.pro discussed at various times throughout rest of the course

Similar documents
Chapter 15 Functional Programming Languages

Universitetet i Linköping Institutionen för datavetenskap Anders Haraldsson

The Halting Problem is Undecidable

Programming Languages in Artificial Intelligence

Roman Numerals Case Study 1996 M. J. Clancy and M. C. Linn

simplicity hides complexity

Basic Lisp Operations

16. Recursion. COMP 110 Prasun Dewan 1. Developing a Recursive Solution

3 Abstract Data Types and Search

SharePoint 2010 Farm Restore

Robot Programming with Lisp

Quick Sort. Implementation

Unix Shell Scripts. Contents. 1 Introduction. Norman Matloff. July 30, Introduction 1. 2 Invoking Shell Scripts 2

Solving Linear Systems, Continued and The Inverse of a Matrix

Grandstream Networks, Inc. UCM6100 Series IP PBX Appliance CDR and REC API Guide

Omega Automata: Minimization and Learning 1

No Solution Equations Let s look at the following equation: 2 +3=2 +7

QUERYING THE COMPONENT DATA OF A GRAPHICAL CADASTRAL DATABASE USING VISUAL LISP PROGRAM

10CS35: Data Structures Using C

CMPSCI 250: Introduction to Computation. Lecture #19: Regular Expressions and Their Languages David Mix Barrington 11 April 2013

The execution of a program written in a high-level language provides an

MATRIX ALGEBRA AND SYSTEMS OF EQUATIONS

Basic numerical skills: EQUATIONS AND HOW TO SOLVE THEM. x + 5 = = (2-2) = = 5. x = 7-5. x + 0 = 20.

How To Program In Scheme (Prolog)

2x + y = 3. Since the second equation is precisely the same as the first equation, it is enough to find x and y satisfying the system

Binary Coded Web Access Pattern Tree in Education Domain

Chapter 5. Recursion. Data Structures and Algorithms in Java

How To Understand The Theory Of Computer Science

DaimlerChrysler EBMX HTTP/s Quick Start Guide

LINEAR INEQUALITIES. less than, < 2x + 5 x 3 less than or equal to, greater than, > 3x 2 x 6 greater than or equal to,

Form & Function in Software. Richard P. Gabriel phd mfa

Solving Systems of Linear Equations

Row Echelon Form and Reduced Row Echelon Form

Quality Center LDAP Guide

Party Time! Computer Science I. Signature Examples PAIR OBJ. The Signature of a Procedure. Building a Set. Testing for Membership in a Set

8 Fractals: Cantor set, Sierpinski Triangle, Koch Snowflake, fractal dimension.

Algorithms and Data Structures

Relations: their uses in programming and computational specifications

Domain Name Resolver (DNR) Configuration

Properties of sequences Since a sequence is a special kind of function it has analogous properties to functions:

CS103B Handout 17 Winter 2007 February 26, 2007 Languages and Regular Expressions

Training Needs Analysis

Predicate Logic. For example, consider the following argument:

HOMEWORK 5 SOLUTIONS. n!f n (1) lim. ln x n! + xn x. 1 = G n 1 (x). (2) k + 1 n. (n 1)!

Settling a Question about Pythagorean Triples

Module 2 Stacks and Queues: Abstract Data Types

Fundamentele Informatica II

1. Give the 16 bit signed (twos complement) representation of the following decimal numbers, and convert to hexadecimal:

Mathematical Induction

6.170 Tutorial 3 - Ruby Basics

Solving simultaneous equations using the inverse matrix

TIBCO ActiveMatrix BusinessWorks Plug-in for Big Data User s Guide

Domain of a Composition

I2B2 TRAINING VERSION Informatics for Integrating Biology and the Bedside

Functional Programming

COMP 356 Programming Language Structures Notes for Chapter 10 of Concepts of Programming Languages Implementing Subprograms.

Lab 1: Introduction to C, ASCII ART and the Linux Command Line Environment

Active Directory User Management System (ADUMS)

Debugging Prolog Programs. Contents The basic Prolog debugger Tracing program execution Spying on problem predicates

Masters programmes in Computer Science and Information Systems. Object-Oriented Design and Programming. Sample module entry test xxth December 2013

Selecting Features by Attributes in ArcGIS Using the Query Builder

Functional Programming. Functional Programming Languages. Chapter 14. Introduction

Chapter 9. Systems of Linear Equations

Cartesian Products and Relations

Guide to Performance and Tuning: Query Performance and Sampled Selectivity

MA4001 Engineering Mathematics 1 Lecture 10 Limits and Continuity

High-Level Programming Languages. Nell Dale & John Lewis (adaptation by Michael Goldwasser)

Incremental Maximum Flows for Fast Envelope Computation

Outline. Introduction Linear Search. Transpose sequential search Interpolation search Binary search Fibonacci search Other search techniques

Web'n'walk. Manager. for Windows USER MANUAL

Automata and Formal Languages

Stanford Math Circle: Sunday, May 9, 2010 Square-Triangular Numbers, Pell s Equation, and Continued Fractions

PMOD Installation on Linux Systems

Interface Definition Language

Course Title: Microsoft Access Basic Duration: 12 hours

Patterns in Pascal s Triangle

Release Notes. Identikey Server Release Notes 3.1

Ad Hoc Advanced Table of Contents

Section IV.1: Recursive Algorithms and Recursion Trees

Presented by Greg Lindsay Technical Writer Windows Server Information Experience. Presented at: Seattle Windows Networking User Group April 7, 2010

Basic Proof Techniques

CS 3719 (Theory of Computation and Algorithms) Lecture 4

Automated Testing Tool

Code_Aster. Procedure DEFI_FICHIER. 1 Drank

Grade descriptions Computer Science Stage 1

The Graphical Method: An Example

Microsoft Access 2007 Advanced Queries

<Insert Picture Here> Designing and Developing Highly Scalable Applications with the Oracle Database

Relational Database: Additional Operations on Relations; SQL

Data Structures. Jaehyun Park. CS 97SI Stanford University. June 29, 2015

PERPETUITIES NARRATIVE SCRIPT 2004 SOUTH-WESTERN, A THOMSON BUSINESS

Software Quality Exercise 1

THEORY of COMPUTATION

A Little Set Theory (Never Hurt Anybody)

Mansfield City Schools ELA Pacing Guide Grade 6 Reading

Thirty Useful Unix Commands

How to speed up IDENTIKEY DNS lookup of the Windows Logon DAWL client on Windows 7?

SharePoint List Filter Webpart User Manual

Testing and Tracing Lazy Functional Programs using QuickCheck and Hat

Reading 13 : Finite State Automata and Regular Expressions

Transcription:

Utility programs In utilities.pro discussed at various times throughout rest of the course UT-1

member ( I, L ) Item I is a member of the list L. > Reduce the list second rule until first in list first rule. or empty no rule so fail member ( X, [ X _ ] ). member ( X, [ _ Z ] ) :- member ( X, Z ). Note the use of the anonymous variable _» We do not care about the value of the rest in the first rule, nor the value of first in the second rule» Typically use it when it is the only instance of that variable in the rule UT-2

append ( L1, L2, R ) R is the result of appending list L2 to the end of list L1. > Cannot redefine in Qunitus Prolog. append ( [], L, L ). Appending to nil yields the original list. append ( [ X L1 ], L2, [ X L3 ] ) :- append (L1, L2, L3 ). > Simultaneous recursive descent on L1 & L3 first of the left list is the first of the result. Pattern L1 = a b c L2 = 2 3 4 5 L3 = a b c 2 3 4 5 X a b c 2 3 4 5 X a b c 2 3 4 5 L1 L2 L3 UT-3

append ( L1, L2, R ) 2 Queries ask for results in all combinations. Not like Java or C where functions are programmed for only one query append ( [ 1, 2, 3 ], [ a, b, c ], R ). > What is the result of appending L1 and L2? append ( L1, [ a, b, c ], [ 1, 2, 3, a, b, c ] ). > What L1 gives [ 1, 2, 3, a, b, c ] when appended with [ a, b, c ]? append ( [ 1, 2, 3 ], L2, [ 1, 2, 3, a, b, c ] ). > What L2 gives [ 1, 2, 3, a, b, c ] when appended to [ 1, 2, 3 ]? UT-4

append ( L1, L2, R ) 3 append ( L1, L2, [ 1, 2, 3, a, b, c ] ). > What L1 and L2 gives [ 1, 2, 3, a, b, c ] when L2 is appended to L1? append ( L1, L2, R ). > What L1 and L2 give R? Infinite number of answers append ( Before, [Middle After], List ). > If middle is defined we can get the before and after append ( Before, [4 After], [1,2,3,4,5,6,7] ). UT-5

Trace append ( P, [ a ], [ 1, 2, 3, a ] ) Variables are renamed every time a rule is used for matching append ( [], L, L ). append ( [ X L1 ], L2, [ X L3 ] ) :- append ( L1, L2, L3 ). Try to match rule 1 P = [] [a] = L_1 [1,2,3,a] = L_1 1 Fail, try to match rule 2 P = [X_2 L1_2] [a] = L2_2 [1,2,3,a] = [X_2 L3_2]» Succeed with X_2 = 1 L2_2 = [a] L3_2 = [2,3,a] UT-6

Trace append ( P, [ a ], [ 1, 2, 3, a ] ) 2 append ( [], L, L ). append ( [ X L1 ], L2, [ X L3 ] ) :- append ( L1, L2, L3 ). Try to match rule 1 append(l1_2, [a], [2,3,a]) L1_2 = [] [a] = L_3 [2,3,a] = L_3 2 Fail, try to match rule 2 L1_2 = [X_4 L1_4] L2_4 = [a] [2,3,a] = [X_4 L3_4]» Succeed with X_4 = 2 L2_4 = [a] L3_4 = [3,a] Try to match rule 1 append(l1_4, [a], [3,a]) L1_4 = [] [a] = L_5 [3,a] = L_5 UT-7

Trace append ( P, [ a ], [ 1, 2, 3, a ] ) 3 append ( [], L, L ). append ( [ X L1 ], L2, [ X L3 ] ) :- append ( L1, L2, L3 ). 3 Fail, try to match rule 2 L1_4 = [X_6 L1_6] [a] = L2_6 [3,a] = [X_6 L3_6]» Succeed with X_6 = 3 L2_6 = [a] L3_6 = [a] Try to match rule 1 append(l1_6, [a], [a]) L1_6 = [] [a] = L_7 [a] = L_7 Succeed, recursion stops, backtrack and substitute values UT-8

Trace append ( P, [ a ], [ 1, 2, 3, a ] ) 4 In step 3 L1 _4 = [ 3 [] ] = [3] In step 2 we had L1_2 = [X_4 L1_4] L2_4 = [a] [2,3,a] = [X_4 L3_4]» Succeed with X_4 = 2 L2_4 = [a] L3_4 = [3,a]» and from Step 3 L1_4 = [3]» Thus L1_2 = [2, 3] In step 1 we had P = [X_2 L1_2] [a] = L2_2 [a,1,2,3] = [X_2 L3_2]» Succeed with X_2 = 1 L2_2 = [a] L3_2 = [2,3,a]» and from Step 2 L1_2 = [2, 3]» Thus P = [1, 2, 3] UT-9

delete ( I, L, R ) R is the result of deleting item I from the list L. delete ( X, [ X Y ], Y ). > Like saying L = ( cons ( car L ) ( cdr L ) ) in Lisp delete ( X, [ Y W ], [ Y Z ] ) :- delete (X, W, Z ). > Check the rest of the list if not the first item. Analogous to ( cons ( car L ) ( recurse ( cdr L ) ) in Lisp UT-10

prefix ( P, L ) P is a prefix of the list L. It can be defined using append as follows. prefix ( P, L ) :- append ( P, _, L ). > P is a prefix of L if something, including nil, can be suffixed to P to form L. UT-11

prefix ( P, L ) 2 We can define prefix in terms of itself as follows. List PPPPPPXXXXX ==> XXXXX Prefix YYYYYY - Empty ^^^^^^ Check equality until Prefix is exhausted. The base case is having the empty list as the prefix. prefix ( [], _ ). The recursive case is having the first items on the prefix and the list being the same and the reduced prefix and list satisfy the prefix property. prefix ( [A B ], [ A C ] ) :- prefix ( B, C ). UT-12

suffix ( S, L ) S is a suffix of the list L. It can be defined using append as follows. suffix ( S, L ) :- append ( _, S, L ). > S is a suffix of L if something, including nil, can be prefixed to S to form L. UT-13

suffix ( S, L ) 2 We can define suffix in terms of itself as follows. List PPPPPPXXXXX ==> XXXXX Suffix YYYYY YYYYY ^^^^^^ Reduce the prefix part of the List. In the base case the suffix is the list. suffix ( L, L ). The recursive case is to reduce the size of the prefix of the list. suffix ( S, [ _ L ] ) :- suffix ( S, L ). UT-14

sublist ( S, L ) S is a sublist of L can be defined using append as follows. sublist ( S, L ) :- append ( _, S, Lt ), append ( Lt, _, L ). > S is a sublist of L if something, including nil, can be prefixed to S to form the list Lt > And something, including nil, can be suffixed to Lt to form L. In other words, S is a sublist of L if there exists a prefix P to S and a suffix T to S such that L = P S T > where means concatenation. UT-15

sublist(s,l) We can define sublist in terms of itself and prefix as follows. List PPPPSSSSSXXXXXX ==> SSSSSXXXXXX Sublist YYYYY YYYYY ^^^^ Reduce the prefix part of the List. In the base case the sublist is the prefix of the list. sublist ( S, L ) :- prefix ( S, L ). The recursive case is to reduce the size of the prefix of the list. sublist (S, [ _ L ] ) :- sublist ( S, L ). UT-16