[comp.lang.prolog] Strings: a bad example.

ok@mudla.cs.mu.OZ.AU (Richard O'Keefe) (11/22/89)

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.

rcm@Apple.COM (Robert Monsen) (11/28/89)

Regarding printing files from prolog, Richard O'Keefe writes
> (Actually,
>         system($more <HELP.HLP$)
>  would *really* have been the best way to handle this...)

Please don't do this. My macintosh doesn't have a "system" to call :-)

Bob Monsen
Orion Network Systems
Disclaimer