Path: utzoo!utgpu!news-server.csri.toronto.edu!rpi!zaphod.mps.ohio-state.edu!usc!apple!agate!ucbvax!isidev.UUCP!nowlin From: nowlin@isidev.UUCP Newsgroups: comp.lang.icon Subject: Re: longstr oops Message-ID: <9103200233.AA07937@uunet.UU.NET> Date: 20 Mar 91 02:33:06 GMT Sender: daemon@ucbvax.BERKELEY.EDU Distribution: inet Organization: The Internet Lines: 44 From-Id: <1991Mar19.213134.15017@midway.uchicago.edu> > So you say I should actually test programs before posting them? > Looks like the while/every mistake eluded more people than just > me, though. It took a confused (or rather not-so-confused) note > from Ken Walker. > > # Find longest match()-ing string in l. Initialize m to -1 so > # as to detect cases where "" is the only match that succeeds. > # > m := -1 # Attempt to match() each member in l (=!l). > every m <:= *(s[i:j] ? =!l) # Produce the length of each match that suc- > # ceeds, and store its value in m if it is > # greater than m's current value. > # > # Return i + the length of the longest match. Fail if there was > # no match (i.e. m still has its original value). > # > return i + (-1 ~= m) The 'while' was not a mistake. The program I posted with test data and a main procedure in it worked just fine with a 'while' instead of an 'every'. I wasn't looking for iteration here. I was looking for GOAL DIRECTED EVALUATION. The 'while' forces the expression: m <:= *(s[i:j] ? =!l) to do everything it can to succeed. Since there's a generator in this expression (!l) all the strings in 'l' are generated every time through the loop until one of the strings is matched and it's longer than the last string that was matched. GOAL DIRECTED EVALUATION forces the generator to generate so you don't need an 'every'. When there are no more strings in 'l' that will match and that are longer than the last match the loop terminates. The 'every' works, but for a different reason. The 'every' is probably faster since it only iterates through the list once. It's important to understand why both work for different reasons. They both work though. +-------------------------------------------------------------------------+ | --- --- | | | S | Iconic Software, Inc. - Jerry Nowlin - uunet!isidev!nowlin | | --- --- | +-------------------------------------------------------------------------+