Prolog Programming for AI, problem 7.1
- From: Lash Rambo <lr@xxxxxxxxxxxx>
- Date: Sun, 20 Aug 2006 03:03:48 GMT
I'm soldiering through "Prolog Programming for Artifical Intelligence" by
Bratko, but have run into a nasty exercise. It's 7.1, and reads:
"
Write a procedure 'simplify' to symbolically simplify summation
expressions with numbers and symbols (lower-case letters). Let the
procedure rearrange the expressions so that all the symbols precede
numbers. These are examples of its use:
?- simplify(1 + 1 + a, E).
E = a + 2
?- simplify(1 + a + 4 + 2 + b + c, E).
E = a + b + c + 7
?- simplify(3 + x + x, E).
E = 2*x + 3
"
The imperative solution sprung immediately to mind. You'd just read
through the expression, use a hash to map each symbol to its number of
occurrences, and tally the constants. At the end, stitch the contents of
the hash together with the tally. That's maybe 5 lines of Perl code.
For Prolog, the best I could come up with was scanning the summation
expression, and for each constant: 1) if it's an integer, add it to the
running integer total, and 2) if it's an atom, delete all occurrences of
that atom in the expression, keeping count of the number of deletions,
and replace them with the new factor. E.g., if you had "3 + x + x",
you'd delete the two xs, leaving "3", and then you'd add the new factor
for the xs: "3 + 2*x". Fart around with ordering the integers, smooth
out the rough spots (what if it's all integers? what if it's all
atoms?), and you're looking at a couple dozen lines of junk. Plus, in
the end what you're left with is a ghastly, inefficient reformulation of
the elegant imperative solution.
Figuring there must be some clever solution to the problem using only
things the book has covered up to this point, I peeked at the solutions
in the back of the book. Sure enough, there was a clever solution--for a
completely different problem! It looks like originally the problem was
to write a partially reversible addition procedure. See, that would have
actually been fun. It'd take a couple minutes and I'd get to use the
type predicates, which was the point of the exercises.
I next did a quick Google Groups search on the problem. I found a thread
from a couple years ago, but all the solutions involved things that
haven't yet been covered in the book. The solutions were also much more
complex than the solutions for any problems up to this point in the book.
As such, I doubt they're what Bratko had in mind.
Finally, I found a simplification procedure tied in with a
differentiation procedure, but even it used more advanced stuff than has
been covered in the book so far.
So, is there a simple, clever solution to this problem in Prolog? I'm
thinking a dozen lines, max. No CFGs, no term decomposition (beyond
stuff like X+Y), no wacky 3rd party libraries, etc.
.
- Follow-Ups:
- Re: Prolog Programming for AI, problem 7.1
- From: growe
- Re: Prolog Programming for AI, problem 7.1
- From: student
- Re: Prolog Programming for AI, problem 7.1
- From: bart demoen
- Re: Prolog Programming for AI, problem 7.1
- From: russell kym horsell
- Re: Prolog Programming for AI, problem 7.1
- From: Greg Buchholz
- Re: Prolog Programming for AI, problem 7.1
- From: Markus Triska
- Re: Prolog Programming for AI, problem 7.1
- Prev by Date: Re: C/C++ written tiny prolog
- Next by Date: XPCE : action when closing a dialog
- Previous by thread: Prolog goals order
- Next by thread: Re: Prolog Programming for AI, problem 7.1
- Index(es):
Relevant Pages
|