Article View: comp.lang.prolog
Article #480A Question of Style
From: Peter Reintjes
Date: Thu, 16 Nov 1989 00:09
Date: Thu, 16 Nov 1989 00:09
72 lines
2911 bytes
2911 bytes
I have been doing some Xwindows programming in Prolog
(thanks to the UCLA XWIP system) and I have written the
same fragment of code three different ways. I would be
interested in comments about clarity and how people
choose between styles of writing Prolog code.
The problem is highly non-logical. Given that
we are refreshing the screen, either because
we are drawing a new picture or re-drawing
the old one because the screen was newly exposed,
we want to flush out all pending Expose events.
Here is the code which handled the Expose events
and refreshes the screen,
service(xEvent(xExpose,_,_,_,Window,_,_,_,_,_),Connection) :-
flush_events(Connection,Window),
refresh.
flush_events(Conn,Win) :-
xGetEvent(Conn,Win,[],xExpose,xTrue,xFalse,_)
-> flush_events(Conn,Win)
; true.
I toyed with this structure:
flush_events(Conn,Win) :-
xGetEvent(Conn,Win,[],xExpose,xTrue,xFalse,_),
!,
flush_events(Conn,Win)
flush_events(_,_).
But it didn't seem worth two clauses, also, I don't
really need a REAL cut when the soft-cut of an
if-then-else would do just fine.
Then I really got to thinking about what is going on,
You see, if the xGetEvent succeeds, it means it got
an event and there might be more. If it fails, then
we are done. Sooo....
repeat,
\+ xGetEvent(Conn,Win,[],xExpose,xTrue,xFalse,_),
!,
Does the trick. It is easy to put in-line (no predicate overhead)
but my initial response to this was negative because I try to
avoid repeat and cut whenever possible. Actually, this fragment
is almost nothing BUT logically "bad" things (repeat, negation, cut).
The upshot of this is -- Since I am supposed to be teaching people
how to program REAL programs in Prolog. Should I *recommend* the
last structure? First I'd like to know which example people prefer?
Any novices out there that can give me a gut reaction?
And maybe implementers can tell me which is more efficient and why.
I frequently have the problem of liking a software trick
because it is "cute" or "minimalist" in some way, but then
being highly suspicious of it as the best way to write code
which is to be read by other people (as all good code is).
Prolog has a much better shot at being a readable than
other languages (APL is too cryptic, while C and other
are too bulky - with lots of code that isn't descriptive
of the problem at hand) and in many cases, the minimal
Prolog solution is the most logical and robust, but when
we are doing non-logical things . . .
What do you think.
Technical Details
Message-ID:
<1471@speedy.mcnc.org>Path:
arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!shadooby!samsung!xanth!mcnc!pbrShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!utgpu!jarvis.csri.toronto.edu!mailrus!shadooby!samsung!xanth!mcnc!pbr From: pbr@mcnc.org (Peter Reintjes) Newsgroups: comp.lang.prolog Subject: A Question of Style Keywords: programming style prolog non-logical Message-ID: <1471@speedy.mcnc.org> Date: 16 Nov 89 00:09:02 GMT Organization: MCNC; RTP, NC Lines: 72