List Manipulation in Prolog

Slides:



Advertisements
Similar presentations
Formal Models of Computation Part II The Logic Model
Advertisements

The Logic of Quantified Statements
PROLOG 8 QUEENS PROBLEM.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
Logic Programming Lecture 3: Recursion, lists, and data structures.
Introduction to Prolog What is Prolog? Application Areas of Prolog How does Prolog work? (Syntax of Prolog) Program Structure.
CSE (c) S. Tanimoto, 2008 Propositional Logic
CSE 221: Probabilistic Analysis of Computer Systems Topics covered: Continuous random variables Uniform and Normal distribution (Sec. 3.1, )
CSE (c) S. Tanimoto, 2005 Logic Programming 1 Logic Programming Outline: Motivation Examples: The Grandmother relation Formulation in Prolog Logic,
CSE S. Tanimoto Introduction 1 CSE 341 Programming Languages Autumn 2001 Instructor: Steve Tanimoto Teaching assistants:
CS 603: Programming Languages Lecture 25 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
Notes for Week 11 Term project evaluation and tips 3 lectures before Final exam Discussion questions for this week.
Principles of programming languages 12: Logic programming Supplemental material Definition of prefix and suffix Isao Sasano Department of Information Science.
CS 403: Programming Languages Lecture 18 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
CSE 341, S. Tanimoto Logic Programming Intro - 1 Logic Programming Introduction Motivation. Sample logic programs.
Done By :- -Nesreen Basem -Sara nabil -Rawan Prolog Program.
CSE (c) S. Tanimoto, 2001 Logic Programming
CSE 341, S. Tanimoto Logic Programming -
Knowledge Representation III First-Order Logic
For Friday No reading Prolog handout 3 Chapter 9, exercises 9-11.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Functions (Notation and Evaluating)
PROGRAMMING IN HASKELL
PROGRAMMING IN HASKELL
CSC 8520 Spring Paula Matuszek
Methods for Evaluating Validity
Fully detailed logic model
كيــف تكتـب خطـة بحـث سيئـة ؟؟
الدكتـور/ عبدالناصـر محمـد عبدالحميـد
The Boolean (logical) data type boolean
Bin Sort, Radix Sort, Sparse Arrays, and Stack-based Depth-First Search CSE 373, Copyright S. Tanimoto, 2002 Bin Sort, Radix.
Knowledge Representation and Inference
كار همراه با آسودگي و امنيت
CSE 415 Introduction to Artificial Intelligence Winter 2004
Notes 9: Inference in First-order logic
First Order Logic: Logical Inference
Introduction to Logic Programming and Prolog
Functions And Function Notation.
Tail Recursion.
CSE 341 Programming Languages Autumn 2001
CSE 341 Programming Languages Autumn 2003
CSE 341 Programming Languages Autumn 2002
Scheme: Basic Functionality
مديريت موثر جلسات Running a Meeting that Works
Introduction to Logic Programming and Prolog
Functions (Notation and Evaluating)
CSE 373, Copyright S. Tanimoto, 2002 Asymptotic Analysis -
List Manipulation in Prolog
Introduction to Prolog
Bin Sort, Radix Sort, Sparse Arrays, and Stack-based Depth-First Search CSE 373, Copyright S. Tanimoto, 2001 Bin Sort, Radix.
PROGRAMMING IN HASKELL
Problem Spaces & Search
Which way does the robot have to turn to get to the charger?
CSE 415 Introduction to Artificial Intelligence Winter 2003
CSE (c) S. Tanimoto, 2004 Logic Programming
CSE 341 Programming Languages Autumn 2003
Introduction to Prolog
CSE (c) S. Tanimoto, 2002 Logic Programming
4.8 Functions and Relations
Operations Research Lecture 7.
LISP: Basic Functionality
Problem Spaces & Search
Introduction to Logic Programming and Prolog
LISP: Basic Functionality
CSE 373, Copyright S. Tanimoto, 2001 Asymptotic Analysis -
LISP: Basic Functionality
Introduction to Logic Programming and Prolog
Function Notation.
PROLOG.
Presentation transcript:

List Manipulation in Prolog A Non-list Example Prolog's List notation List manipulation techniques Append Running definitions forwards and backwards CSE 341 -- S. Tanimoto Lists in Prolog

CSE 341 -- S. Tanimoto Lists in Prolog Example % courses.pl Some prerequisite relationships prereq(cse142, cse143). prereq(cse143, cse341). prereq(cse341, cse473). before(X, Y) :- prereq(X, Y). before(X, Y) :- prereq(X, Z), before(Z, Y). CSE 341 -- S. Tanimoto Lists in Prolog

CSE 341 -- S. Tanimoto Lists in Prolog A Session ?- consult(courses). % courses compiled 0.00 sec, 1,088 bytes Yes ?- prereq(cse142, cse143). ?- prereq(cse142, X). X = cse143 ; No CSE 341 -- S. Tanimoto Lists in Prolog

CSE 341 -- S. Tanimoto Lists in Prolog A session (cont) ?- prereq(X, Y). X = cse142 Y = cse143 ; X = cse143 Y = cse341 ; X = cse341 Y = cse473 ; No ?- CSE 341 -- S. Tanimoto Lists in Prolog

CSE 341 -- S. Tanimoto Lists in Prolog A session (cont) ?- before(X, cse341). X = cse143 ; X = cse142 ; No CSE 341 -- S. Tanimoto Lists in Prolog

CSE 341 -- S. Tanimoto Lists in Prolog colors([r, g, b]). ?- colors(L). L = [r, g, b] ?- colors([X|Y]). X = r, Y = [g, b] % X is the head. Y is the tail. ?- mylist([a, [b, c]]). CSE 341 -- S. Tanimoto Lists in Prolog

Defining Predicates on Lists car([X|L], X). cdr([X|L], L). cons(X, L, [X|L]). ?- car([a, b, c], X). X = a ?- cdr([a, b, c], X). X = [b, c] ?- cons(a, [b, c], X). X = [a, b, c] CSE 341 -- S. Tanimoto Lists in Prolog

CSE 341 -- S. Tanimoto Lists in Prolog concat a.k.a. append concat([], L, L). concat([X|L1], L2, [X|L3]) :- concat(L1, L2, L3). ?- concat([a, b, c], [d, e], X). X = [a, b, c, d, e] ?- concat(X, [d, e], [a, b, c, d, e]). X = [a, b, c] CSE 341 -- S. Tanimoto Lists in Prolog

CSE 341 -- S. Tanimoto Lists in Prolog concat used backwards ?- concat(X, Y, [x, y, z]). X = [] Y = [x, y, z] ; X = [x] Y = [y, z] ; X = [x, y] Y = [z] ; X = [x, y, z] Y = [] ; No CSE 341 -- S. Tanimoto Lists in Prolog

Logic Prog. vs Functional Prog. Functional programming: evaluation is one-way. Logic programming: evaluation can be two-way. CSE 341 -- S. Tanimoto Lists in Prolog