[comp.lang.icon] longstr oops

nowlin@isidev.UUCP (03/20/91)

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  |
|  --- ---                                                                |
+-------------------------------------------------------------------------+

sbw@TURING.CSE.NAU.EDU (Steve Wampler) (03/20/91)

On Mar 19 at 19:35, isidev!nowlin@uunet.uu.net writes:
} 
} 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.

Along the same lines, if the list were sorted by length of strings
(longest first), you can eliminate the 'while' and the 'every' and
let's GDE do it's thing.  It just doesn't seem worth it do so...

And anyway, I definitely like Bob Alexander's solution more.

-- 
	Steve Wampler
	{....!arizona!naucse!sbw}
	{sbw@turing.cse.nau.edu}