[mercury-users] Leveraging Perl

Richard A. O'Keefe ok at atlas.otago.ac.nz
Wed May 2 08:48:43 AEST 2001


Peter Schachte <schachte at cs.mu.OZ.AU> wrote:
	One thing you can't do as easily in Mercury is write a simple text file
	filter.  For example, take a .html file containing cities and their current
	weather observations as a big table, and extract and print out the
	information for a particular city.  This is the kind of thing that's pretty
	quick and easy to hack together in Perl.
	
This is also the kind of thing that is terribly easy to hack WRONG in Perl.
By far the simplest way to do it would be to use SWI Prolog, with its
SGML parser that lets you slurp in HTML or most other SGML applications
(*not* just XML) as a Prolog data structure and then process it as a tree.

I would far rather write something like

    desendant(element(table,_,Rows), Document),
    member(element(tr,_,[element(th,_,['Paris'])|ParisInfo]), Rows)

than any analogous Perl text.  SOmeone really should port the SWI SGML
parser (there's a C core that does everything plus glue to adapt it to
SWI Prolog) to Mercury.

Oh yes,
    descendant(X, X).
    descendant(X, element(_,_,Children)) :-
        member(Child, Children),
        descendant(X, Children).

A very serious problem with using Perl is that it encourages you to treat
structured data as strings, and thereby get them wrong.  I have seen plenty
of Perl scripts for poking around in C sources, for example, but never yet
seen one that got C lexical syntax right.

	Another thing Mercury is not as good at as Perl is rapid prototyping.  If
	you were writing a text processing application in a speed programming
	contest, would you choose Perl or Mercury?  I'd take Perl every time.
	
I might choose Haskell (types rather like Mercury but normally inferred,
with the advantage of the QuickTest library) or Smalltalk (superlative IDE,
SUnit testing kit), to be honest.  The only "speed programming contests" I
know anything about are the national/regional/international ACM programming
contests, and to win in those your program must produce CORRECT answers.

(For what it's worth, I solved one year's national programming contest
problems in C and Haskell.  The Haskell code was dramatically shorter.
Perl has C-like syntax.  Case closed.)
--------------------------------------------------------------------------
mercury-users mailing list
post:  mercury-users at cs.mu.oz.au
administrative address: owner-mercury-users at cs.mu.oz.au
unsubscribe: Address: mercury-users-request at cs.mu.oz.au Message: unsubscribe
subscribe:   Address: mercury-users-request at cs.mu.oz.au Message: subscribe
--------------------------------------------------------------------------



More information about the users mailing list