Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!uwm.edu!bionet!agate!ucbvax!isidev.UUCP!nowlin From: nowlin@isidev.UUCP Newsgroups: comp.lang.icon Subject: Re: RE: terrible code Message-ID: <9103190245.AA11982@uunet.uu.net> Date: 19 Mar 91 02:45:15 GMT Sender: daemon@ucbvax.BERKELEY.EDU Distribution: inet Organization: The Internet Lines: 49 >From article <1991Mar18.171946.28280@midway.uchicago.edu> (Richard L. Goerwitz) > In article <9103181558.AA20389@uunet.uu.net> nowlin@isidev.UUCP writes: > > > > m := 0 > > while *(p := (s ? =!l)) > m do m := *p > > return m + 1 > > Very clever. Try matching each member of l, keeping a record of the > length of the match. The longest match wins. > > This sort of code does exactly what my code does. Here's the problem. > Solution of the type exemplified above involve mindless interation > through the entire list, l. I decided that it would be sensible to > ... > There must be a way to do the kinds of things we're talking about here > in Icon, and do it with somewhat greater speed than the =!l approach. I've included the source from a subsequent reposting of the piece of code being discussed since it was modified to follow the original program more closely: m := 0 while *(p := (s[i:j] ? =!l)) > m do m := *p if /p then fail else return i + m The key to this is that it's not a "mindless iteration through the entire list". It's an iteration, but in any language but Icon you'd have to explicitly do a lot more to control this iteration than in the simple expression above. Simple is in the eyes of the beholder :-) Any expression that follows the 'while' control structure must try to succeed due to goal directed evaluation. That means if the expression contains a generator, in this case the !l, results are generated until the generator is exhausted or the expression succeeds in the context of one of the results. If the expression succeeds the result is used to modify the expression. Eventually none of the generator's results will cause the expression to succeed and the loop is exited. This Icon stuff is pretty slick. I fail to see why a few of dozen lines of admittedly "terrible" code make a better solution than these four lines. I could do a matching table in C that would blow this away in terms of speed but this is Icon. It should be done Iconishly. +-------------------------------------------------------------------------+ | --- --- | | | S | Iconic Software, Inc. - Jerry Nowlin - uunet!isidev!nowlin | | --- --- | +-------------------------------------------------------------------------+