1 member(X,[Y| _ ] ) :- X = Y. member(X, [ _ | Y]) :- member(X, Y). It would be easier to write this as: member(X,[X| _ ]). member(X, [ _ | Y]) :- member(X,

Slides:



Advertisements
Similar presentations
Artificial Intelligence: Natural Language and Prolog
Advertisements

Constructing a prolog program We will learn predicates for: – input and output. – Modifying the knowledge base. – Controlling backtracking process.
1 Input/Output and Debugging  How to use IO Streams  How to debug programs  Help on coursework.
1 Input/Output and Debugging  How to use IO Streams  How to debug programs.
Prolog The language of logic. History Kowalski: late 60’s Logician who showed logical proof can support computation. Colmerauer: early 70’s Developed.
11/10/04 AIPP Lecture 6: Built-in Predicates1 Combining Lists & Built-in Predicates Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture.
1 Introduction to Prolog References: – – Bratko, I., Prolog Programming.
Introduction to ProLog File Manipulations Prof. Dr. Nagi Soliman 2009.
1 Logic Programming. 2 A little bit of Prolog Objects and relations between objects Facts and rules. Upper case are variables. parent(pam, bob).parent(tom,bob).
Lecture 16 More on Lists1 asserta(X). assertz(X). assert(X). retract(X). retractall(X).
1Lecture 12 Introduction to Prolog SWI-Prolog. 2Lecture 12 Introduction to Prolog Welcome to SWI-Prolog (Version 4.0.9) Copyright (c) University.
INTRODUCTION TO PROLOG. PROLOG BASICS Atoms - most primitive terms that the language manipulates start with lower case letter includes strings (‘inside.
About prolog  History  Symbolic Programming Language  Logic Programming Language  Declarative Programming Language.
1 Prolog’s Lists, Negation and Imperative Control Flow COMP 144 Programming Language Concepts Spring 2003 The University of North Carolina at Chapel Hill.
1 Prolog III. 2 Lists [ ] is the empty list. [x, 2+2, [a, b, c]] is a list of three elements. The first element in the list is its “head”. The list with.
TCP1211-Logic Programming Control and Side Effects Programming Faculty of Information Technology Multimedia University.
Side effects in Prolog Lecturer: Xinming (Simon) Ou CIS 505: Programming Languages Fall 2010 Kansas State University 1.
Pure Prolog vs. Extralogical Prolog Pure Prolog programs can be interpreted as logical statements about what they compute. Pure Prolog programs can be.
PROLOG I/O I/O and the relational model. PROLOG I/O operators. –Not really predicates. I/O with characters. I/O with terms. I/O with lists. I/O.
FATIH UNIVERSITY Department of Computer Engineering Input and Output Notes for Ch.6 of Bratko For CENG 421 Fall03.
For Friday Read “lectures” 1-5 of Learn Prolog Now: prolog-now/
Part 1 The Prolog Language Chapter 6 Input and Output
Carlos Varela Rennselaer Polytechnic Institute September 10, 2007
Lists – More Examples  ‘Translate’ a list to another list  Split a given list into two  Merge two lists into one  Counting  Output a list (some I/O.
How to Program in C++ CHAPTER 3: INPUT & OUTPUT INSTRUCTOR: MOHAMMAD MOJADDAM.
1 COMP 144 Programming Language Concepts Felix Hernandez-Campos Lecture 28: Prolog’s Lists, Negation and Imperative Control Flow COMP 144 Programming Language.
04/11/04 AIPP Lecture 12: I/O1 Input/Output Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 12 04/11/04.
(9.1) COEN Logic Programming  Logic programming and predicate calculus  Prolog statements  Facts and rules  Matching  Subgoals and backtracking.
P-Phunck - Prolog1 Introduction to Prolog Brian Paden.
1 Prolog I. 2 Syllogisms “Prolog” is all about programming in logic. –Socrates is a man. –All men are mortal. –Therefore, Socrates is mortal.
CS 321 Programming Languages and Compilers Prolog part 2.
Lists – More Examples  ‘Translate’ a list to another list  Counting  Split a given list into two  Merge two lists into one  Output a list (some I/O.
1 Lecture Expert Systems &. 2 Operator Notation A programmer can define new operators by inserting into the program special kinds of clauses,
CS 326 Programming Languages, Concepts and Implementation Instructor: Mircea Nicolescu Lecture 7.
14/10/04 AIPP Lecture 7: The Cut1 Controlling Backtracking: The Cut Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 7 14/10/04.
15/11/04 AIPP Lecture 14: Database Manipulation1 Database Manipulation Artificial Intelligence Programming in Prolog Lecturer: Tim Smith Lecture 14 15/11/04.
For Wednesday Read “lectures” 7-10 of Learn Prolog Now Chapter 9, exs 4 and 6. –6 must be in Horn clause form Prolog Handout 2.
1 COMP 205 Introduction to Prolog Dr. Chunbo Chu Week 14.
Functions. Built-in functions You’ve used several functions already >>> len("ATGGTCA")‏ 7 >>> abs(-6)‏ 6 >>> float("3.1415")‏ >>>
1 Prolog and Logic Languages Aaron Bloomfield CS 415 Fall 2005.
1Lecture 12 Introduction to Prolog Logic Programming Prolog.
Rule-based Programming, Logic Programming and Prolog.
CS 603: Programming Languages Lecture 25 Spring 2004 Department of Computer Science University of Alabama Joel Jones.
Lecture 9b: Prolog Heshaam Faili University of Tehran The language of logic Concepts Examples.
Summary. likes(tom,jerry). likes(mary,john). likes(tom,mouse). likes(tom,jerry). likes(jerry,cheeze). likes(mary,fruit). likes(john,book). likes(mary,book).
F28PL1 Programming Languages Lecture 18: Prolog 3.
Logic Programming Chapter 15 Part 2. Breadth-first v Depth-first Search Suppose a query has compound goals (several propositions must be satisfied) Depth-first.
CS 403: Programming Languages Lecture 18 Fall 2003 Department of Computer Science University of Alabama Joel Jones.
Lecture course: Heuristics & Prolog for Artificial Intelligence Applications Notes on Prolog from Professor Bruce Batchelor, DSc.
MB: 26 Feb 2001CS Lecture 11 Introduction Reading: Read Chapter 1 of Bratko Programming in Logic: Prolog.
For Monday Exam 1 is Monday Takehome due Prolog Handout 3 due.
1 Artificial Intelligence CS370D Prolog programming List operations.
For Friday No reading Prolog Handout 2. Homework.
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.
COP4020 Programming Languages Logic Programming Prof. Xin Yuan.
CS Class 04 Topics  Selection statement – IF  Expressions  More practice writing simple C++ programs Announcements  Read pages for next.
1 Lecture 16: Prolog’s Lists, Negation and Imperative Control Flow (Section 11.3) CSCI 431 Programming Languages Fall 2002 A modification of slides developed.
Artificial Intelligence Programming in Prolog
Section 16.5, 16.6 plus other references
For Wednesday Read “lectures” 7-10 of Learn Prolog Now:
Built-in Predicates cs774 (Prasad) L8BuiltIns.
Prolog Lists.
Chapter 3: Input/Output
Chapter 3: Prolog (Lists, Arithmetic, Operators)
Programming Paradigms and Languages
Prolog III.
Prolog Lists.
Chapter 2: Prolog (Introduction and Basic Concepts)
Prolog III Lists 8-Jul-19.
Presentation transcript:

1 member(X,[Y| _ ] ) :- X = Y. member(X, [ _ | Y]) :- member(X, Y). It would be easier to write this as: member(X,[X| _ ]). member(X, [ _ | Y]) :- member(X, Y). ?- member(1, [3,4,5,8,1,9]). Yes ?- member(X, [prolog, c, ada, haskell]). X= prolog; X= c X= ada; X= haskell; No Recursion and Lists

2 Other Examples change(you, i). change(are, [am, not]). change(french, australian). change(do, no). change(X, X). /* catchall */ alter([ ], [ ]). alter([H|T], [X|Y]) :- change(H, X), alter(T,Y). ?- alter([you,are,a,computer],R). R = [i, [am, not], a, computer] Yes ?- alter([you,are,french],R). R = [i, [am, not], australian] Yes ?-

3 asserta/1. assertz/1 assert/1 retract/1 retractall/1 Example: assertz(fib(N,F)).

4 :-dynamic fib fib(1,1). fib(2,1). fib(N,F) :- N > 2, N1 is N-1, fib(N1,F1), N2 is N-2, fib(N2,F2), F is F1 + F2, asserta(fib(N,F)). ?- fib(8, F). F = 21 ?- fib(6,F). F = 8

5 F(6) + f(5)f(4) + f(3) f(2) f(2) f(1) f(4) f(3) + f(3) f(2) 1 + f(2) f(1)

6 asserta/1. assertz/1 assert/1 retract/1 retractall/1 Example: assertz(fib(N,F)).

7 :-dynamic animal/1. % A directive. animal(tiger). animal(lion). animal(monkey). animal(X):-mamal(X), asserta(animal(X)). mamal(cat). mamal(dog). ?- animal(dog). Yes ?- listing(animal). :- dynamic animal/1. animal(dog). animal(tiger). animal(lion). animal(monkey). animal(A) :- mamal(A), asserta(animal(A)).

8 leng([ ], 0). leng([H|T], Len) :- leng(T, Len1), Len is Len Finding the length of a list

9 [trace] ?- leng([1,2,3,4,5],Ln). Call: (6) leng([1, 2, 3, 4, 5], _G405)?creep Call: (7) leng([2, 3, 4, 5], _G474) ? creep Call: (8) leng([3, 4, 5], _G474) ? creep Call: (9) leng([4, 5], _G474) ? creep Call: (10) leng([5], _G474) ? creep Call: (11) leng([], _G474) ? creep Exit: (11) leng([], 0) ? creep ^ Call: (11) _G479 is 0+1 ? creep ^ Exit: (11) 1 is 0+1 ? creep Exit: (10) leng([5], 1) ? creep ^ Call: (10) _G482 is 1+1 ? creep ^ Exit: (10) 2 is 1+1 ? creep Exit: (9) leng([4, 5], 2) ? creep ^ Call: (9) _G485 is 2+1 ? creep ^ Exit: (9) 3 is 2+1 ? creep Exit: (8) leng([3, 4, 5], 3) ? creep ^ Call: (8) _G488 is 3+1 ? creep ^ Exit: (8) 4 is 3+1 ? creep Exit: (7) leng([2, 3, 4, 5], 4) ? creep ^ Call: (7) _G405 is 4+1 ? creep ^ Exit: (7) 5 is 4+1 ? creep Exit: (6) leng([1, 2, 3, 4, 5], 5) ? creep Ln = 5 Yes leng([ ], 0). leng([H|T], Len) :- leng(T, Len1), Len is Len1 + 1.

10 remove(X, [ ], [ ]). remove(X, [X|T], C):- remove(X,T,C). remove(X, [H|T], [H|T1]):- X \= H, remove(X,T,T1). ?- remove(1,[4,9,8,7,1,9,7,5,1],R). R = [4, 9, 8, 7, 9, 7, 5] Removing an element from a list

11 isort([], []). isort([H|T],R):- isort(T,Q), ins(H,Q,R). ins(X, [], [X]). ins(X, [H|T], [H|R]):- X>H, ins(X,T,R). ins(X, [H|T], [X,H|T]):- X=<H. ?- isort([4,1,9,5,8,3,2],S). S = [1, 2, 3, 4, 5, 8, 9] ?- isort([a,b,c,d],S). ERROR: Arithmetic: `c/0' is not a function Insertion Sort

12 I/O

13 A full-stop must follow a term to be read ?- read(X). |: massey. X = massey Yes ?- read(X). |: Y. X = _G185 Yes ?- read(X). |: likes(john,mary). X = likes(john, mary) Yes ?- read(X), name(X,List). |: massey. X = massey List = [109, 97, 115, 115, 101, 121]

14 ?- write('hello world'). hello world Yes ?- write('hello world'),nl,nl,tab(9),write('I am here :)'). hello world I am here :) Yes ?- write("abc"). [97, 98, 99] Yes ?- write('abc'). abc Yes

15 printlst([ ]) :- put('.'), nl. printlst([H|T]) :- write(H), tab(1), printlst(T). ?- write([‘This’,is,a,list]). [This, is, a, list] ?- printlst([‘This’,is, a,list]). This is a list. ?- read(X),nl,write(‘Here is what was entered: ‘),write(X). |: massey. Here is what was entered: massey X = massey A predicate for printing a list

16 male(andrew). male(john). male(george). male(greg). male(adam). female(mary). female(jennifer). female(eve). parents(john,george,mary). parents(greg,adam,eve). parents(jennifer, adam,eve). parents(andrew, adam,eve). is_brother_of(X,Y):-male(X),parents(X, Father, Mother),parents(Y, Father,Mother), X\=Y,write(X),tab(1),write('is'),tab(1),write(Y),write('s sister.'). is_sister_of(X,Y):-female(X),parents(X, Father, Mother),parents(Y, Father, Mother), X\=Y,write(X),tab(1),write('is'),tab(1),write(Y),write('s sister.'). ?- is_sister_of(X,Y). jennifer is greg`s sister. X = jennifer Y = greg ; jennifer is andrew`s sister. X = jennifer Y = andrew

17 Built-in predicates “fail” and “true” Fail always fails and true always succeeds ?- likes(mary,X). X = john Yes ?- likes(mary,X),fail. No ?- likes(mary,X),write(X),nl,fail. john fruit book No ?- likes(mary,X),write(X),nl,fail ; true. john Fruit OR book X = _G354 Yes likes(tom,jerry). likes(mary,john). likes(tom,mouse). likes(tom,jerry). likes(jerry,cheeze). likes(mary,fruit). likes(john,book). likes(mary,book). likes(tom,john). Output predicates do not re-succeed on backtracking

18 likes(tom,jerry). likes(mary,john). likes(tom,mouse). likes(tom,jerry). likes(jerry,cheeze). likes(mary,fruit). likes(john,book). likes(mary,book). likes(tom,john). ?- likes(mary,X), write('Mary likes '), write(X),put('.'), nl, fail. Mary likes john. Mary likes fruit. Mary likes book. No ?- likes(mary,X),write(X),nl,fail ; write('reached the OR part'). john fruit book reached the OR part X = _G471 Yes

19 write('Enter a number X to be raised to the power of 5 '), read(N), P5 is N**5,write(N),write(‘ to the power of 5 is: '),write(P5). ?- write('Enter a number X to be raised to the power of 5: '), read(N), | P5 is N**5,write(N),write(' to the power of 5 is: '),write(P5). Enter a number X to be raised to the power of 5: |: 2. 2 to the power of 5 is: 32 N = 2 P5 = 32 Yes ?- ?- repeat,write('Enter a number X to be raised to the power of 5: '), | read(N), P5 is N**5,write(N),write(' to the power of 5 is: '), | write(P5),fail. Built-in predicate “repeat”

20 File I/O

21 Prolog accepts input from an input stream send output to an output stream Streams can be files or I/O devices Active streams are the keyboard and the screen by default (user) We may only have one stream open for input and one open for output

22 tell(Filename) Opens the file named by Filename for writing and makes it the current output stream. It creates the file if it does not exist. telling(X) Retrieves the current input stream. told Closes the file opened by the built-in predicate tell. see(Filename) Opens the file named by Filename for input and makes it the current input straeam. seeing(X) Retrieves the current input stream. seen Closes the file opened by the built-in predicate see.

23 ?- tell('D:/test.txt'). Yes ?- telling(X). X = '$stream'( ) Yes ?- write('testing tell'). Yes ?- told. Yes testing tellend_of_file Contents of D:/test.txt ?- telling(Old),tell(‘D:/test.txt’),write(‘testing’), told,tell(Old).

24 ?- tell(‘lksout.txt’),likes(mary,X), write(‘Mary likes ‘),write(X), put(‘.’),nl, fail; told. X = _G662 Yes likes(tom,jerry). likes(mary,john). likes(tom,mouse). likes(tom,jerry). likes(jerry,cheeze). likes(mary,fruit). likes(john,book). likes(mary,book). likes(tom,john). Mary likes john. Mary likes fruit. Mary likes book. Contents of the file “lks.txt”

25 Cut: ! Eliminates choices Always succeeds but stops backtracking a:-b,c,!,d. a:-e,f. max(X,Y,Y) :- Y>X. max(X,Y,X). ?- max(1,2,X). X = 2 ; X = 1 ; No ?- max(X,Y,Y) :- Y>X, !. max(X,Y,X). ?- max(1,2,X). X = 2 ; No ?-

26  Cut makes programs less readable.  Using cut can make your programs more efficient.  You have to be careful when using cut. Cut, may alter the way your program behaves. When you use cut in a rule, you have to be certain of how your rule will be used. If you are not careful your program may behave strangely. Built-in predicate cut, “!”

27 Unexpected results: Using member with cut we only get one value for X, which is not what we expect. member1(X,[X|_]):-!. member1(X,[_|T]):-member1(X,T). ?- trace. Yes [trace] ?- member1(X,[1,2,3,4]). Call: (6) member1(_G401, [1, 2, 3, 4]) ? creep Exit: (6) member1(1, [1, 2, 3, 4]) ? creep X = 1 ; Fail: (6) member1(1, [1, 2, 3, 4]) ? creep No X = 1 ; X = 2 ; X = 3 ; X = 4 ; No Member without cut membernocut(X,[X|_]). membernocut(X,[_|T]):-membernocut(X,T). ?- membernocut(X,[1,2,3,4]).

28 Efficiency: Using member without cut here, we are wasting time trying out the second part of the predicate when we know that it will fail. Call: (7) membernocut(1, [1, 2, 3, 4]) ? creep Exit: (7) membernocut(1, [1, 2, 3, 4]) ? creep member succeeded Redo: (7) membernocut(1, [1, 2, 3, 4]) ? creep Call: (8) membernocut(1, [2, 3, 4]) ? creep Call: (9) membernocut(1, [3, 4]) ? creep Call: (10) membernocut(1, [4]) ? creep Call: (11) membernocut(1, []) ? creep Fail: (11) membernocut(1, []) ? creep Fail: (10) membernocut(1, [4]) ? creep Fail: (9) membernocut(1, [3, 4]) ? creep Fail: (8) membernocut(1, [2, 3, 4]) ? creep Fail: (7) membernocut(1, [1, 2, 3, 4]) ? creep No membernocut(X,[X|_]). membernocut(X,[_|T]):-membernocut(X,T). ?- trace. Yes [trace] ?- membernocut(1,[1,2,3,4]),write('member succeeded'),fail.

29 Using member with cut: ?- trace. Yes [trace] ?- member1(1,[1,2,3,4]),write('member succeeded'),fail. Call: (7) member1(1, [1, 2, 3, 4]) ? creep Exit: (7) member1(1, [1, 2, 3, 4]) ? creep member succeeded member1(X,[X|_]):-!. member1(X,[_|T]):-member1(X,T). Fail: (7) member1(1, [1, 2, 3, 4]) ? creep No ?-

30 member(X, [X|T]). member(X, [H|T]) :- not(X=H), member(X, T). Using not instead of cut makes your program more clear member1(X,[X|_]):-!. member1(X,[_|T]):-member1(X,T).

31 male(andrew). male(john). male(george). male(greg). male(adam). female(mary). female(jennifer). female(eve). parents(john,george,mary). parents(greg,adam,eve). parents(jennifer, adam,eve). parents(andrew, adam,eve). is_brother_of(X,Y):-male(X),parents(X, Father, Mother),parents(Y, Father,Mother), X\=Y. is_sister_of(X,Y):-female(X),parents(X, Father, Mother),parents(Y, Father, Mother),X\=Y. ?- male(X). X = andrew ; X = john ; X = george ; X = greg ; X = adam ; No ?- ?- male(X), !. X = andrew ; No

32 male(andrew). male(john). male(george). male(greg). male(adam). female(mary). female(jennifer). female(eve). parents(john,george,mary). parents(greg,adam,eve). parents(jennifer, adam,eve). parents(andrew, adam,eve). is_brother_of(X,Y):-male(X), !,parents(X, Father, Mother), parents(Y, Father,Mother), X\=Y. is_sister_of(X,Y):-female(X), !,parents(X, Father, Mother), parents(Y, Father, Mother),X\=Y.

33 apend([],L,L):-!. apend([H|T1],L2,[H|T2]):-apend(T1,L2,T2). [trace] ?- apend([],[1,2,3,4],L). Call: (6) apend([], [1, 2, 3, 4], _G409) ? creep Exit: (6) apend([], [1, 2, 3, 4], [1, 2, 3, 4]) ? creep L = [1, 2, 3, 4] ; Fail: (6) apend([], [1, 2, 3, 4], [1, 2, 3, 4]) ? creep No apend([],L,L). apend([H|T1],L2,[H|T2]):-apend(T1,L2,T2). [trace] ?- apend([],[1,2,3,4],L). Call: (6) apend([], [1, 2, 3, 4], _G409) ? creep Exit: (6) apend([], [1, 2, 3, 4], [1, 2, 3, 4]) ? creep L = [1, 2, 3, 4] ; Fail: (6) apend([], [1, 2, 3, 4], _G409) ? creep No

34 ?- tell(‘fam.pl’), listing(male/1), told. Yes male(andrew). male(john). male(george). male(greg). male(adam). Contents of the file fam consult(Filename) :- see(Filename), repeat, read(X), assert(X), X=end_of_file, !, seen. Definition of “consult” H

35 male(andrew). male(john). male(george). male(greg). male(adam). female(mary). female(jennifer). female(eve). parents(john,george,mary). parents(greg,adam,eve). parents(jennifer, adam,eve). parents(andrew, adam,eve). ?- male(X). X= andrew; X= john ; X= george ; X= greg ; X= adam ; ?- female(X). X= mary; X= jennifer; X= eve; ?- parents(X, adam, eve). X= greg; X= jennifer; X= andrew; ?- findall(X, male(X), List). List= [andrew, john, george, greg, adam] ?- findall(X, female(X), List). List= [mary, jennifer, eve] ?- findall(X, parents(X,adam,eve), List). List= [greg, jennifer, andrew] findall(X,Term,List).

36 Other examples ?- findall(t(X,Y),parents(X,Y,Z),List). X = _G353 Y = _G354 Z = _G358 List = [t(john, george), t(greg, adam), t(jennifer, adam), t(andrew, adam)] Yes ?- ?- findall(t(X,Y),append(X,Y,[a,b]),L). X = _68 Y = _69 L = [t([],[a,b]),t([a],[b]),t([a,b],[])] yes male(andrew). male(john). male(george). male(greg). male(adam). female(mary). female(jennifer). female(eve). parents(john,george,mary). parents(greg,adam,eve). parents(jennifer, adam,eve). parents(andrew, adam,eve).

37 call(Goal) Invoke Goal as a goal. ?- call(a=a). Yes ?- [user]. |: likes(mary,john). |: % user compiled 0.02 sec, 152 bytes Yes ?- call(likes(mary,john)). Yes ?- call(likes(mary,X)). X = john Yes ?- call(likes(X,Y)). X = mary Y = john Yes ?- call(likes(X,mary)). No ?-

38 ?- not(likes(mary,john)). No ?- not(likes(mary,X)). No ?- not(likes(mary,george)). Yes ?- not(Goal)Succeeds when Goal fails. Implementation of not using call not(P):-call(P),!,fail. not(P).

39 In Prolog, programs and data are made of terms. A term may be: a constant (an atom or a number) 10, ‘abc’, likes, mary a variable X, Y a compound term likes(mary,john), likes(X,Y). There are several built-in predicates that succeed or fail depending on the type of the term their argument is. atomic(X) Succeeds if X is bound to an atom, a string or a number (integer or floating point). atom(X)Succeeds if X is bound to an atom. number(X) Succeeds if X is bound to a number (integer or float) float(X) Succeeds if X is bound to a floating point number

40 ?- atom(a). Yes ?- atom(X). No ?- X=a,atom(X). X = a Yes ?- X=a,atomic(X). X = a Yes ?- atomic(a). Yes ?- atomic(likes). Yes ?- atom(likes). Yes ?- atom(1). No ?- atom(1.2). No ?- atomic(1.2). Yes ?- number(5). Yes ?- number(1.2). Yes ?- float(1). No ?- float(1.1). Yes

41 An example Counting the number of occurances of an atom count(Atom,List,N). count( _, [ ], 0 ). count(Atom, [Atom | Tail], N):- count(Atom,Tail,N1), N is N count(Atom,[ _ | Tail], N):- count(Atom,Tail,N). ?- count(1,[1,2,3,1],N). N = 2 Yes ?- count(1,[1,X,Y,Z],N). X = 1 Y = 1 Z = 1 N = 4 % incorrect Yes ?- ?- L= [1,2,3,X,2],write(L),count(2,L,N). [1, 2, 3, _G368, 2] L = [1, 2, 3, 2, 2] X = 2 N = 3 % incorrect Yes ?-

42 Solution count( _, [ ], 0 ). count(Atom,[Head | Tail], N):- atomic(Head), Atom=Head, count(Atom,Tail,N1), N is N1 + 1;count(Atom,Tail,N). ?- count(1,[1,2,3,1],N). N = 2 Yes ?- count(1,[1,X,Y,Z],N). X = _G269 Y = _G272 Z = _G275 N = 1 Yes ?- ?- L= [1,2,3,X,2],write(L),count(2,L,N). [1, 2, 3, _G371, 2] L = [1, 2, 3, _G371, 2] X = _G371 N = 2 Yes ?-