1 Artificial Intelligence CS370D Prolog programming List operations.

Slides:



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

More Prolog test vs. find built-in predicates list operations: member, append, nth0, reverse, … not, default vs. logical negation comparison operators,
ArrayLists The lazy mans array. Whats the matter here? int[] list = new int[10]; list[0] = 5; list[2] = hey; list[3] = 15; list[4] = 23;
Lisp. Versions of LISP Lisp is an old language with many variants Lisp is alive and well today Most modern versions are based on Common Lisp LispWorks.
Notes for CS3310 Artificial Intelligence Part 5: Prolog arithmetic and lists Prof. Neil C. Rowe Naval Postgraduate School Version of July 2009.
CHAPTER 4 AND 5 Section06: Sequences. General Description "Normal" variables x = 19  The name "x" is associated with a single value Sequence variables:
Declarative Programming Lists in PROLOG Autumn 2014.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/5.
Chapter Three: Lists, Operators, Arithmetic CS 461.
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.
LING 388: Language and Computers Sandiway Fong Lecture 5: 9/8.
1 Programming Languages and Paradigms Lisp Programming.
String and Lists Dr. Benito Mendoza. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list List.
1 TP #3:List and Database manipulation n Last TP exercises solved; n Lists and their predefined predicates; n Prolog as a Database: predefined predicates.
Commands and predicates LISP functions are divided into 2 classes. Predicates are functions that return boolean values i.e. t or nil. The rest are commands.
Lisp. Versions of LISP Lisp is an old language with many variants –LISP is an acronym for List Processing language Lisp is alive and well today Most modern.
CS 330 Programming Languages 11 / 20 / 2007 Instructor: Michael Eckmann.
Fourteen lists and compound data. Some primitive data types Integers (whole numbers) 1, 2, 3, -1, 0, , etc. “Floating-point” numbers ,
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Lists, Operators, Arithmetic Notes for Ch.3 of Bratko For CSCE 580 Sp03 Marco.
1 Lists (Ref. Brna’s book pp26-32, 53-63)  Unifications (by examples)  Representation of Lists  Recursion  Manipulating Lists  Some Prolog Built-in.
Children under 12 can enter the museum at no charge. Write the inequality x < 12 If you are 12 can you get in free? no If you are 11 can you get in free?
LISP 1.5 and beyond A very quick tour. Data Atoms (symbols) including numbers – All types of numbers including Roman! (well, in the early days) – Syntactically.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 9 More About Strings.
Basic Lisp CIS 479/579 Bruce R. Maxim UM-Dearborn.
For Monday Read Chapter 3 Homework: –Lisp handout 2.
COP4020 Programming Languages Logical programming with Prolog Prof. Xin Yuan.
LING 388: Language and Computers Sandiway Fong Lecture 6.
Basic Operators. What is an operator? using expression is equal to 9. Here, 4 and 5 are called operands and + is the operator Python language supports.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
Built-in Data Structures in Python An Introduction.
Chapter Three: Lists, Operators, Arithmetic 1. © Patrick Blackburn, Johan Bos & Kristina Striegnitz Important things about lists  List elements are enclosed.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
Lisp Functional Language or Applicative Language –Achieves its effect by applying functions, either recursively or through composition Powerful, expressive,
Lecture 19 - More on Lists, Slicing Lists, List Functions COMPSCI 101 Principles of Programming.
Copyright © 2012 Pearson Education, Inc. Publishing as Pearson Addison-Wesley C H A P T E R 8 Lists and Tuples.
Daniel Jung. Types of Data Structures  Lists Stacks Queues  Tuples  Sets  Dictionaries.
LISP Data Types Functional Programming Academic Year Alessandro Cimatti
Inequalities. SymbolMeaning Greater Than =Equal Greater Than or Equal To.
UNIVERSITI TENAGA NASIONAL CMPB454 ARTIFICIAL INTELLIGENCE (AI) CHAPTER 6 LOGIC PROGRAMMING USING PROLOG CHAPTER 6 LOGIC PROGRAMMING USING PROLOG Instructor:
Artificial Intelligence
Lists in Prolog Sections 3.1, 3.2. Lists n List = sequence of values –[1, 2, 3, 4, 5] –[bob, brian, cathy, mark, david, loretta] –[birds(4, calling),
Perl Variables: Array Web Programming1. Review: Perl Variables Scalar ► e.g. $var1 = “Mary”; $var2= 1; ► holds number, character, string Array ► e.g.
XPath --XML Path Language Motivation of XPath Data Model and Data Types Node Types Location Steps Functions XPath 2.0 Additional Functionality and its.
In The Name Of Allah Lab 03 1Tahani Aldweesh. objectives Searching for the solution’s. Declaration. Query. Comments. Prolog Concepts. Unification. Disjunction.
LISTS and TUPLES. Topics Sequences Introduction to Lists List Slicing Finding Items in Lists with the in Operator List Methods and Useful Built-in Functions.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
INLS 560 – S TRINGS Instructor: Jason Carter. T YPES int list string.
Computer Eng. Software Lab II , Semester 2, Who I am: Andrew Davison CoE, WiG Lab Office Functional Programming.
11/2/00SEM107 © Kamin & ReddyClass 6 - Lists - 1 Class 6 - Recursion on Lists r More recursive methods on lists.
Spring 16 CSCI 4430, A Milanova/BG Ryder 1 Announcements HW5 due March 28 Homework Server link is up I will have office hours, Fri or Mon, check Announcements.
String and Lists Dr. José M. Reyes Álamo. 2 Outline What is a string String operations Traversing strings String slices What is a list Traversing a list.
String and Lists Dr. José M. Reyes Álamo.
Class 2 – Recursion on Lists
Containers and Lists CIS 40 – Introduction to Programming in Python
Solving Equations with the Variable on Each Side
Lists Part 1 Taken from notes by Dr. Neil Moore & Dr. Debby Keen
Vectors.
Artificial Intelligence CS370D
String and Lists Dr. José M. Reyes Álamo.
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Topics Basic String Operations String Slicing
CA 208 Logic Ex9 Define the following list processing predicates in Prolog: member(X,L) % if X is a member of list L last_member(X,L) %
Topics Basic String Operations String Slicing
Variables and Equations
Lisp.
List Operation in ProLog EUSL/TC/IS/2013/COM/08. In here list will print..
Topics Basic String Operations String Slicing
Terminology and Symbols
Presentation transcript:

1 Artificial Intelligence CS370D Prolog programming List operations.

Outline: List operations: - Membership. - Appending. - Reversing. - Select predicate. - Deleting an item from list. - Length of a list. 2

3 Membership operation: A common predicate when manipulating lists is a membership test: Is a given value a member of a list? The member built-in predicate takes two arguments. If the first argument is any term except a variable and the second argument is a list. If the first argument is an variable, it is bound to an element of the list working to find all the elements of a list in turn from left to right. member succeeds if the first argument is a member of the list denoted by the second argument (i.e. one of its list elements).

4 Membership operation examples: Examples: ?-member(maha, [laila,maha,sara]). true ?- member(dog, [cat,[dog,horse]]). false ?- member([apple,orange], [banana,[apple,orange]]). true ?- member(x,[]). false ?- member([1,2,3],[a,b,[1,2,3],c]). true ?- member(X,[a,b,c]). X =a ;X = b ;X = c.

5 Append operation: The append built-in predicate takes three arguments: append(L1,L2,L3). - If the first two arguments L1,L2 are lists and the third argument is an variable, the third argument is bound to a list comprising the first two lists concatenated. - If the all arguments are lists, then the predicate check if the last argument L3 contains all element of L1 and L2 and then return true or false. - If the first two arguments are variables and the last argument is a list then try to divide the list between these variables.

6 Append operation examples: Examples: ?- append([1,2,3,4],[5,6,7,8,9],L). L = [1,2,3,4,5,6,7,8,9] ?- append([],[1,2,3],L). L = [1,2,3] ?- append([[a,b,c],d,e,f],[g,h,[i,j,k]],L). L = [[a,b,c],d,e,f,g,h,[i,j,k]]

7 Append operation examples: (cont) Examples: ?- append([a,b],[c,d],[a,b,c,d]). yes ?- append([a,b],[c,d],[a,b,a,c,d]). no ?- append([],[b,c],[b,c,d]). no ?-append([a,[b,c],d],[a,b],L). L = [a, [b, c], d, a,b].

8 Append operation examples: (cont) Examples: ?- append(L1,L2,[1,2,3]). L1 = [],L2 = [1,2,3] ; default L1 = [1],L2 = [2,3] ; L1 = [1,2],L2 = [3] ; L1 = [1,2,3],L2 = []. ?- L = [1,2,3,4,5], append(L1,[_,_,_],L). L = [1, 2, 3, 4, 5], L1 = [1, 2]

9 Reversing a list: The term reverse the elements in L1 and put them reversed in L2 variable: reverse(L1,L2). If all arguments are lists then check if the second argument reversed from the first then return true of false. reverse(List1,List2).

10 Reversing a list: Examples: ?- reverse([1,2,3,4], What). What=[4,3,2,1] ?- reverse([a,b,c],[c,b,a]). true

11 Select predicate: This predicate check if L2 has the same items in L1 after element removed as the same order. select(element,L1,L2). Examples: ?- select(a,[a,c],[a,d]). false. ?- select(1,[1,2,3],[3,2]). false. Not the same order. ?- select(a,[a,b,c],[b,c]). true. ?- select(a,[b,c],[b,c]). false. The first list does not contain the item.

12 Delete item from list: The term delete the element fromL1 where L2 is equal to L1 with the item X removed delete(L1, element,L2). Examples: ?- delete([a,b,c],a,L). L = [b, c]. ?- delete([a,b,c],g,H). H = [a, b, c]. ?- delete([a,b,a,a],a,L). L = [b].

13 Length of a list: length (List, N) This term used to count the elements in a list List and put the numbers in the N variable. Examples: ?- length([a,b,c],N). N = 3 ?- length([],M). M = 0. ?- length([a,b,c],5). False.

14 Reference: More predicates of SWI prolog lists can found here: