Article View: comp.lang.prolog
Article #513Strings: a bad example.
From: Richard O'Keefe
Date: Wed, 22 Nov 1989 09:45
Date: Wed, 22 Nov 1989 09:45
81 lines
3230 bytes
3230 bytes
I just saw another Prolog program which (mis-)uses strings.
I've translated the program in order to further disguise its origin.
write_help_label(Attribute, RuleNo) :-
string_append($I want to know the value of $, Attribute, X1),
string_append(X1, $ in order to satisfy rule $, X2),
number_string(RuleNo, X3),
string_append(X2, X3, X4),
write(X4), nl.
The thing to notice here is that we have no interest in the values
X1, X2, X3, X4, we merely want to write out a certain sequence. So
write_help_label(Attribute, RuleNo) :-
write($I want to know the value of $),
write(Attribute),
write($ in order to satisfy rule $),
write(RuleNo),
nl.
will do the job without creating four useless strings. You can even use
write_help_label(Attribute, RuleNo) :- % NU Prolog
format('I want to know the value of ~w in order to satisfy rule ~w~n',
[Attribute,RuleNo]).
or
write_help_label(Attribute, RuleNo) :- % LPA MacProlog
writeseqnl(['I want to know the value of ',Attribute,
' in order to satisfy rule ',RuleNo]).
these turn over some list space, but nowhere near as much space as the
string-hacking version, and both can easily be open-coded by a compiler
(which is not to say that either will be).
The same program contained a particularly appalling misuse of strings.
There is a help file, and it is written out like this:
show_the_help_file :-
open($HELP.HLP$, read, Stream),
read_contents(Stream, "", String),
close(Stream),
write(String).
read_contents(Stream, String0, String) :- % actually, I'm lying
( end_of_file(Stream) -> % about this predicate.
String = String0 % it was _much_ worse.
; read_line(Stream, Line),
append_string(String0, Line, String1),
read_contents(Stream, String1, String)
).
This not only reads the whole file in as a string but does so using
a quadratic cost algorithm. Also, it limits the help file to being
no longer than a string, whatever that limit is. It could have been
written as
show_the_help_file :-
seeing(OldInput),
see('HELP.HLP'),
repeat,
get0(C),
( C >= 0 -> put(C), fail ; true ),
!,
see(OldInput),
close('HELP.HLP').
or any of several other methods which never create any strings at all,
and don't impose any bound on the size of the file being displayed.
(Actually,
system($more <HELP.HLP$)
would *really* have been the best way to handle this...)
These are just two examples which show why I do not like strings and would
not include them in my ideal programming language. Alas, there are enough
Prolog dialects with strings, and enough programmers silly enough to use
strings, that strings _have_ to be in a Prolog standard in order to avoid
breaking the programs that those unfortunates wrote. Pity, really. If
the standard had come out four years ago when it was supposed to, we might
have been in time to educate people about what to do instead of strings.
Technical Details
Message-ID:
<2781@munnari.oz.au>Path:
arc1-ca.pugleaf.net!utzoo!attcan!uunet!munnari.oz.au!mudla!okShow Full Headers
Complete Article Headers
Path: arc1-ca.pugleaf.net!utzoo!attcan!uunet!munnari.oz.au!mudla!ok From: ok@mudla.cs.mu.OZ.AU (Richard O'Keefe) Newsgroups: comp.lang.prolog Subject: Strings: a bad example. Message-ID: <2781@munnari.oz.au> Date: 22 Nov 89 09:45:28 GMT Sender: news@cs.mu.oz.au Lines: 81