🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.lang.prolog
Article #985

A little template matcher

#985
From: Ken Johnson
Date: Mon, 16 Jul 1990 13:24
48 lines
2028 bytes
% A simple template matcher. This is not likely to be of much interest to
% Prolog wizards, but some beginners might find it interesting.
% See examples for how it works. Arg1 to match/2 is a template, arg2 is
% a piece of text, and the object of the code is to instantiate variables
% in the template so that it `matches' the text.
% Ken Johnson, July 1990

test1 :-
	match([the,quick,brown,fox],[the,quick,brown,fox]).

test2(X) :-					% X = [quick]
	match([the,X,brown,fox],[the,quick,brown,fox]).

test3(X) :-					% X = [quick,brown]
	match([the,X,fox],[the,quick,brown,fox]).

test4(X) :-						% X is null
	match([the,quick,brown,X,fox],[the,quick,brown,fox]).

test5(X) :-						% X = [a,rose]
	match([X,is,X,is,X],[a,rose,is,a,rose,is,a,rose]).

test6(X) :-						% FAILS correctly
	match([X,is,X,is,X],[fat,'freddy''s',cat,is,a,rose,is,a,rose]).



match([],[]).

match([X1|T1],Text) :-
	matchv(X1,A-A,T1,Text).

match([X1|T1],[X2|T2]) :-	% The nonvar avoids extra solutions in
	nonvar(X1),		% cases like match([X,b,c],[a,b,c])
	match(T1,T2),		% by avoiding the solution X=a
	X1 = X2.		% (X=[a] is the only solution intended)


matchv(Stuff,Stuff-[],Template,Text) :-
	match(Template,Text).

matchv(X,Stuff-[Word|A],Template,[Word|Text]) :-
	matchv(X,Stuff-A,Template,Text).
-- 
Ken Johnson, AI Applications Institute, 80 South Bridge, Edinburgh EH1 1HN
E-mail ken@aiai.ed.ac.uk, phone 031-225 4464 extension 212
`You can prove things and then people have to believe you, whether they
like you or not' [Hans Eysenck]
Technical Details
Message-ID: <2994@skye.ed.ac.uk>
Path: arc1-ca.pugleaf.net!utzoo!utgpu!watserv1!watmath!att!rutgers!uwm.edu!rpi!dali.cs.montana.edu!uakari.primate.wisc.edu!sdd.hp.com!usc!elroy.jpl.nasa.gov!jarthur!uunet!mcsun!ukc!edcastle!aiai!ken

Show Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!watserv1!watmath!att!rutgers!uwm.edu!rpi!dali.cs.montana.edu!uakari.primate.wisc.edu!sdd.hp.com!usc!elroy.jpl.nasa.gov!jarthur!uunet!mcsun!ukc!edcastle!aiai!ken
From: ken@aiai.ed.ac.uk (Ken Johnson)
Newsgroups: comp.lang.prolog
Subject: A little template matcher
Message-ID: <2994@skye.ed.ac.uk>
Date: 16 Jul 90 13:24:03 GMT
Reply-To: ken@aiai.UUCP (Ken Johnson)
Followup-To: comp.lang.prolog
Organization: Bugs-R-Us
Lines: 48