Re: Exposing Perl programmers to logic programming
[email protected] (Adrian Howard) Fri, 11 Mar 2005 15:57:07 +0000
| Newsgroups | perl.ai |
|---|---|
| Message-ID | <[email protected]> |
On 8 Mar 2005, at 19:18, Ovid wrote:
[snip]
My question in a nutshell: given 45 minutes to show a roomful of
programmers with no prior experience in logic programming just how
powerful it can be, what approach would you take?
[snip]
I've used this little example to impress people (shamelessly stolen
from <http://www.ainewsletter.com/newsletters/aix_0305.htm>.)
Start off with
member( X, [ X | _ ] ).
member( X, [ _ | Z ] ) :- member( X, Z ).
which is enough to demonstrate the declarative and generative stuff you
can get out of simple statements, then hit them with:
send( Object, Message ) :-
class( Object, methods( Methods ) ),
member( ( Message :- Method ), Methods ),
call( Method ).
class(
rectangle( H, W ),
methods( [
( area(A) :- A is H * W ),
( perimeter(P) :- P is 2 * (H + W) )
] )
).
class(
circle(R),
methods( [
( area(A) :- A is pi * R * R ),
( perimeter(P) :- 2 * pi * R )
] )
).
allowing us to do:
?- send( circle(1), area(A) ).
A = 3.14159
Implementing a basic OO system in 6 lines of code is kinda neat :-)
Cheers,
Adrian