Does this Prolog newbie getting it right?
From: Emre Sevinc (emres_at_bilgi.edu.tr)
Date: 12/23/04
- Previous message: reader: "Re: nth0 again"
- Next in thread: vannoord_at_let.rug.nl: "Re: Does this Prolog newbie getting it right?"
- Reply: vannoord_at_let.rug.nl: "Re: Does this Prolog newbie getting it right?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Date: Thu, 23 Dec 2004 21:08:23 +0200
Hi,
Regular readers/posters may be used to my adventures
in Prologwonderland. Once again I want Prolog masters
to enlighten me and enhance my vision. This time a simple
code review (a code that works fine, I guess ;-) will
just do.
I keep on going along the Brakto's book. I've just finished
reading the chapter about non-deterministic finite automata.
He gives example code that can check if a string is acceptable
given a finite state automaton:
final(s3).
trans(s1, a, s1).
trans(s1, a, s2).
trans(s1, b, s1).
trans(s2, b, s3).
trans(s3, b, s4).
silent(s2, s4).
silent(s3, s1).
accepts(State, []) :- % Accept empty string if it is from Final state
final(State).
accepts(State, [X | Rest]) :-
trans(State, X, State1),
accepts(State1, Rest).
accepts(State, String, MaxMoves) :-
silent(State, State1),
accepts(State1, String).
And then he draws attention that the automaton graph can
include cyclic silent paths. If we simply add another transition:
silent(s1, s3).
we'll have a silent cyclic path which can lead to an infinite
loop in case we ask for ?- accepts(s1, [a]).
So Mr. Bratko asks how to handle that situation. My solution is as
follows, modifying the first two accept predicates:
accepts(State, [], MaxMoves) :- % Accept empty string if it is from Final state
final(State).
accepts(State, [X | Rest], MaxMoves) :-
MaxMoves >= 0,
MaxMoves1 is MaxMoves - 1,
trans(State, X, State1),
accepts(State1, Rest, MaxMoves1).
accepts(State, String, MaxMoves) :-
MaxMoves >= 0,
MaxMoves1 is MaxMoves - 1,
silent(State, State1),
accepts(State1, String, MaxMoves1).
I've tried it and it works, what I ask is I did
it fine, if I did The Right Thing. Can it be made
simpler, is it suitable with Prolog spirit, if not
how can it be done?
Thanks in advance.
-- Emre Sevinc eMBA Software Developer Actively engaged in: http:www.bilgi.edu.tr http://ileriseviye.org http://www.bilgi.edu.tr http://fazlamesai.net Cognitive Science Student http://cazci.com http://www.cogsci.boun.edu.tr
- Previous message: reader: "Re: nth0 again"
- Next in thread: vannoord_at_let.rug.nl: "Re: Does this Prolog newbie getting it right?"
- Reply: vannoord_at_let.rug.nl: "Re: Does this Prolog newbie getting it right?"
- Messages sorted by: [ date ] [ thread ] [ subject ] [ author ]
Relevant Pages
|
|