When to use facts instead of rules?
- From: Lash Rambo <lr@xxxxxxxxxxxx>
- Date: Sun, 06 Aug 2006 00:08:56 GMT
I'm just getting started in Prolog by working my way through "Prolog
Programming for Artificial Intelligence" by Bratko. It's all making sense
on a superficial level, but I'm having trouble determining when to use
facts instead of rules. For instance, in problem 2.8, you're to rewrite
this formula without semicolons:
translate(Number, Word) :-
Number = 1, Word = one;
Number = 2, Word = two;
Number = 3, Word = three.
My answer was:
translate(Number, Word) :- Number = 1, Word = one.
translate(Number, Word) :- Number = 2, Word = two.
translate(Number, Word) :- Number = 3, Word = three.
However, the book suggested the more elegant:
translate(1, one).
translate(2, two).
translate(3, three).
I see why that's also correct, but I wouldn't have thought to express it
that way. Part of the problem, I figure, is that I'm coming from 20 years
of programming nothing but imperative languages. The rule version seems
far more obvious to me, I guess, because it looks like a subroutine. The
fact version doesn't look like it "does" anything.
Are there some rules* of thumb to use in determining whether to use a fact
or a rule? Of course, there's the obvious "try to express everything as a
fact first, and if you can't, express it as a rule," but that feels very
unnatural, like speaking a second language by imagining sentences in your
native langauge, then attempting to translate them to the second language.
It works, but it's slow and tiring. I have a hard time believing anyone
who enjoys Prolog programming endures such a thing!
* no pun intended
.
- Follow-Ups:
- Re: When to use facts instead of rules?
- From: russell kym horsell
- Re: When to use facts instead of rules?
- From: michael . goodrich
- Re: When to use facts instead of rules?
- From: Michael D. Kersey
- Re: When to use facts instead of rules?
- From: Bill Spight
- Re: When to use facts instead of rules?
- From: Cesar Rabak
- Re: When to use facts instead of rules?
- Prev by Date: Re: Higher order prolog (code reuse)
- Next by Date: Re: When to use facts instead of rules?
- Previous by thread: Higher order prolog (code reuse)
- Next by thread: Re: When to use facts instead of rules?
- Index(es):
Relevant Pages
|
|