FATIH UNIVERSITY Department of Computer Engineering Programming Style and Technique Notes for Ch.8 of Bratko For CENG421&553 Fall03.

Slides:



Advertisements
Similar presentations
1 Logic Programming School of Informatics, University of Edinburgh append/3 A Drosophila of L.P. append([], L, L) append([ H | T ], L, [ H | R ])  append(T,
Advertisements

C-LISP. LISP 2 Lisp was invented by John McCarthy in 1958 while he was at the Massachusetts Institute of Technology (MIT).John McCarthyMassachusetts Institute.
1 Scheme and Functional Programming Aaron Bloomfield CS 415 Fall 2005.
8 Queens. Problem: Placing 8 queens on a chessboard such that they don’t attack each other Three different Prolog programs are suggessted as solutions.
PROLOG 8 QUEENS PROBLEM.
Modern Programming Languages
Chapter Three: Lists, Operators, Arithmetic 1. Chapter three: 3.1Representation of lists 3.2Some operations on lists 2.
Getting started with ML ML is a functional programming language. ML is statically typed: The types of literals, values, expressions and functions in a.
Functional Programming. Pure Functional Programming Computation is largely performed by applying functions to values. The value of an expression depends.
Lambda Calculus and Lisp PZ03J. Lambda Calculus The lambda calculus is a model for functional programming like Turing machines are models for imperative.
Magician or Math-a-magician?. Math Magic Math Magic – Trick #1 Pick a number… any number! (keep it a secret though) Add 1 to that number Multiply by.
1 Part 1 The Prolog Language Chapter 8 Programming Style and Technique.
Political Map of Europe. 1. British Isles 2. Nordic Nations 3. Central Western Europe 4. Mediterranean Europe 5. Eastern Europe.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CSCE 580 Sp03 Marco Valtorta.
Constraint Logic Programming Ryan Kinworthy. Overview Introduction Logic Programming LP as a constraint programming language Constraint Logic Programming.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using Definite Knowledge: Lists and Difference Lists Notes for Ch.3 of Poole.
Functional programming: LISP Originally developed for symbolic computing Main motivation: include recursion (see McCarthy biographical excerpt on web site).
CSC321: Programming Languages14-1 Programming Languages Tucker and Noonan Chapter 14: Functional Programming 14.1 Functions and the Lambda Calculus 14.2.
Functional programming: LISP Originally developed for symbolic computing First interactive, interpreted language Dynamic typing: values have types, variables.
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Using Structures: Example Programs Notes for Ch.4 of Bratko For CSCE 580 Sp03.
Alternative Programming Paradigms
UNIVERSITY OF SOUTH CAROLINA Department of Computer Science and Engineering Operations on Data Structures Notes for Ch.9 of Bratko For CSCE 580 Sp03 Marco.
Overview Part 2 – Circuit Optimization 2-4 Two-Level Optimization
The Euro 16 Countries (Expand to 23). The Euro and the US Dollar.
16 out of 27 member states Known as euro zones 2 nd largest traded currency after the dollar The name euro was officially adopted on 16 December 1995.
Data Structures Using C++ 2E Chapter 6 Recursion.
FATIH UNIVERSITY Department of Computer Engineering Controlling Backtracking Notes for Ch.5 of Bratko For CENG 421 Fall03.
Data Structures Using C++ 2E Chapter 6 Recursion.
Department of Computer Engineering
FATIH UNIVERSITY Department of Computer Engineering Using Structures: Example Programs Notes for Ch.4 of Bratko For CENG 421 Fall03 Zeynep Orhan.
Functional Programming in Scheme
331 Final Spring Details 6-8 pm next Monday Comprehensive with more emphasis on material since the midterm Study example finals and midterm exams.
0 REVIEW OF HASKELL A lightening tour in 45 minutes.
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
THE EUROPEAN UNION. Member States The European Union is composed of 27 independent sovereign states which are know as member states: Austria, Belgium,
Control Algorithms 1 Chapter 6 Control Algorithms 1 Chapter 6 Pattern Search.
Data Structures Using C++ 2E1 Recursion and Backtracking: DFS Depth first search (a way to traverse a tree or graph) Backtracking can be regarded as a.
NextLastEurope. NextLastEurope  The region of Europe is the area on the map shaded dark purple. Europe.
Europe Research PowerPoint Each group (2-3) must choose two countries from Europe and create a PPT that teaches their classmates about those nations.
Prolog Program Style (ch. 8) Many style issues are applicable to any program in any language. Many style issues are applicable to any program in any language.
Logic Programming and Prolog Goal: use formalism of first-order logic Output described by logical formula (theorem) Input described by set of formulae.
Social Studies: Europe & Russia Lesson 34 Practice & Review
The Schengen Agreement And its implications on tourism.
Discrete Mathematics Lecture # 22 Recursion.  First of all instead of giving the definition of Recursion we give you an example, you already know the.
The 19 th of June 1990 : The Schengen Implementing Convention has been signed by : - Germany - Belgium - France - Luxembourg - Netherlands Calendar.
Computer Class – Summer 20092/21/2016 3:45 AM European Countries Albania Andorra Austria Belarus Belgium Bosnia and Herzegovina Bulgaria Croatia Czech.
07/10/04 AIPP Lecture 5: List Processing1 List Processing Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 5 07/10/04.
Northern Europe Label the following countries on the next page, using the color each countries is labeled in, then add capitals to each country using a.
CS 152: Programming Language Paradigms February 12 Class Meeting Department of Computer Science San Jose State University Spring 2014 Instructor: Ron Mak.
Maps of Topic 2B Multilingualism in Europe Europe A Story of Empire (a united Europe) & Language.
CONFIDENTIAL 1 EPC, European Union and unitary patent/UPC EPC: yes EEA: no EU: no (*) (*) Also means no unitary patent Albania, Macedonia, Monaco, San.
AllianceSymbol Alliance 1:  Strong military  Weak economy  Unlimited government (dictator) Alliance 2:  Strong military  Strong economy  Limited.
CS314 – Section 5 Recitation 9
What does the EU do? Who is in the EU?
A lightening tour in 45 minutes
Closures and Streams cs784(Prasad) L11Clos
EUROS Identification Austria - Belgium - Cyprus - Estonia - Finland - France - Germany - Greece - Ireland - Italy - Latvia Lithuania - Luxembourg - Malta.
Programming Style and Technique
The European Parliament – voice of the people
The European Parliament – voice of the people
The European Union United in Diversity.
Functional Programming
Lecture #8 מבוא מורחב.
European Union Membership
Overview Part 2 – Circuit Optimization
Chapter 8: International Groupings History of the EU: Timeline
Announcements Quiz 5 HW6 due October 23
Name the country marked with an “X.”
Prodcom Statistics in Focus
Presentation transcript:

FATIH UNIVERSITY Department of Computer Engineering Programming Style and Technique Notes for Ch.8 of Bratko For CENG421&553 Fall03

FATIH UNIVERSITY Department of Computer Engineering Use

FATIH UNIVERSITY Department of Computer Engineering Use of Recursion: maplist Recursion is always used. maplist is an example of a “higher- order function”: a function that applies to another function In the original LISP (McCarthy, 1960): ((label maplist (lambda (x f) (cond ((null x) ‘( )) (‘t (cons (f (car x)) (maplist (cdr x) f)))))) maplist( List, F, NewList) if NewList contains the result of applying F to each element of List, in order The function F the binary relation representing the function we want to apply

FATIH UNIVERSITY Department of Computer Engineering maplist II Boundary (base) case: List = [ ] –if List = [ ] then NewList = [ ] General case: List = [ X|Tail] –To transform a list of the form [ X|Tail], do transform the item X by F, obtaining New X, and transform the list Tail obtaining NewTail the whole transformed list if [ NewX|NewTail] See ch8_1.pl Prolog  recursion  objects recursive

FATIH UNIVERSITY Department of Computer Engineering /* maplist( List,F,NewList) if NewList contains the result of applying the function F (represented by a binary relation ) to each entry of NewList, in order. */ maplist( [],_,[]). maplist( [ X|Tail],F,[ NewX|NewTail]) :- G =.. [ F,X,NewX], G, % some Prologs require call( G) instead of G maplist( Tail,F,NewTail). /* Square function, for example query ?- maplist( [2,6,5],square,Squares) */ square( X,Y) :- Y is X*X.

FATIH UNIVERSITY Department of Computer Engineering Generalization Generalization allows us to use recursion Example: eight queens without generalization, no integer to recur on! Define nqueens( Pos,N) –if N queens are safe in position Pos Base case: N = 0: trivial General case: N > 0 achieve a safe configuration of N-1 queens add the remaining queen so that it does attack eightqueens( Pos) :- nqueens( Pos,8).

FATIH UNIVERSITY Department of Computer Engineering Debugging Most Prolog interpreters and compilers use a four- port model for goals foo/n call succeed retry fail

FATIH UNIVERSITY Department of Computer Engineering Improving Efficiency Eight queens, again: using a more informed initial ordering of queen coordinate values helps, as described in the comments at the end of ex4_6.pl Map coloring –start with countries that have many neighbors Warnsdorff’s heuristic for the knight’s tour

FATIH UNIVERSITY Department of Computer Engineering % Ex 4.6[B] % Figure 4.7 Program 1 for the eight queens problem. % solution( BoardPosition) if % BoardPosition is a list of non-attacking queens solution( [] ). solution( [X/Y | Others] ) :- % First queen at X/Y, other queens at Others solution( Others), member( Y,[1,2,3,4,5,6,7,8]), % Order of alternatives defined here! noattack( X/Y, Others). % First queen does not attack others noattack( _, [] ). % Nothing to attack noattack( X/Y, [X1/Y1 | Others] ) :- Y =\= Y1, % Different Y-coordinates Y1-Y =\= X1-X, % Different diagonals Y1-Y =\= X-X1, noattack( X/Y, Others). % A solution template template( [1/Y1,2/Y2,3/Y3,4/Y4,5/Y5,6/Y6,7/Y7,8/Y8] ). solution1(S) :- template(S), solution(S). % When computing all solutions, all orders are equivalent: /* Original Order: member( Y,[1,2,3,4,5,6,7,8]) 4 ?- time(setof(S,solution1(S),L)), length(L,N). % 171,051 inferences in 0.22 seconds ( Lips) */ /* More informed order: member( Y,[1,3,5,7,2,4,6,1]) 16 ?- time(setof(S,solution1(S),L)), length(L,N). % 171,051 inferences in 0.22 seconds ( Lips) */ % When computing one solution, the informed order is much better: /* More informed order: member( Y,[1,3,5,7,2,4,6,1]) 18 ?- time(solution1(S)). % 368 inferences in 0.00 seconds (Infinite Lips) */ /* Original Order: member( Y,[1,2,3,4,5,6,7,8]) 19 ?- time(solution1(S)). % 9,382 inferences in 0.01 seconds ( Lips) */

FATIH UNIVERSITY Department of Computer Engineering colors( []). colors( [ Country/Color|Rest]) :- colors( Rest), member( Color,[yellow,blue,red,green]), not( (member( Country1/Color,Rest),neighbor( Country,Country1)) ). % Note the extra parentheses, to make a single conjunctive goal of two neighbor( Country,Country1) :- ngb( Country,Neighbors), member( Country1,Neighbors). makelist( List) :- collect( [germany],[],List). collect( [],Closed,Closed). % No more candidates for closed collect( [ X|Open],Closed,List) :- member( X,Closed),!, % If X has already been collected, collect( Open,Closed,List). % discard it collect( [X|Open],Closed,List) :- ngb( X,Ngbs), conc( Ngbs,Open,Open1), % built-in version of concat collect( Open1,[ X|Closed],List). country( X) :- ngb( X,_). % Incomplete ngb relation below gives only six countries

FATIH UNIVERSITY Department of Computer Engineering % Neighbors for the 6 original EU countries (Treaty of Rome, 1957) % Andorra, Monaco, San Marino, and Vatican City are not considered % Only land (bridge and tunnel included) borders are considered /* Reorder the ngb relation so that the country that has the largest amount of neighbour will be at the top. So start coloring from that, continue with its neighbours, then the neighbours of the neighbours etc. */ ngb( germany, [denmark,poland,czech,austria,switzerland,france,luxembourg,belgium,netherlands]). ngb( netherlands, [germany,belgium,france]). ngb( belgium, [netherlands,germany,luxembourg,france]). ngb( france, [belgium,luxembourg,germany,switzerland,italy,spain]). ngb( luxembourg, [belgium,germany,france]). ngb( italy, [france,austria,slovenia]). ngb( denmark, [germany]). ngb( poland, [germany]). ngb( czech, [germany]). ngb( austria, [germany, italy]). ngb( switzerland, [germany,italy,france]). ngb( spain, [france]). ngb( slovenia, [italy]).

FATIH UNIVERSITY Department of Computer Engineering Difference Lists The difference list L-E contains the elements of L that occur before E. E is a suffix of L. E.g., [a,b,c,d] – [c,d] is [a,b] Trick: [a,b,c,d|E] – E is [a,b,c,d]! Difference lists allow concatenation of lists in constant time, as in imperative languages.

FATIH UNIVERSITY Department of Computer Engineering Concatenation and Difference Lists Difference lists can be concatenated in constant time by using conc_dl: ?conc_dl([a,b|A] – A, [c,d|C] – C, Result) unify with conc_dl(X – Y, Y – Z, X – Z): {X / [a,b|A]} {Y / A} {A / [c,d|C]} {Z / C} {Result / X – Z}, i.e. {Result / [a,b|A] – C}, i.e. {Result / [a,b,c,d|C} – C}

FATIH UNIVERSITY Department of Computer Engineering Some Caveats While the result of conc_dl has an uninstatiated tail, the first argument does not. –You cannot use it as an argument to conc_dl in the same clause. “Difference lists are powerful but counterintuitive. They appear most often in procedures that were first implemented with conventional lists and then carefully rewritten for efficiency” [Covington, p.183].

FATIH UNIVERSITY Department of Computer Engineering Improveing efficiency by asserting derived facts fib and fib2. Execution trees Page 204 More difficult but more efficient one:forwardfib