Jeremy Cowgar wrote:
> I am new to Icon, so this is most certainly my fault, but I am not
> understanding where to go from here. I have a rather contorted matching
> function that takes a variable number of arguments. It's not necessary
> to go into that. Here is what I would like to accomplish:
>
> if (a is null OR a == data[1])
> AND (b is null OR b == data[2])
> AND (c is null OR c == data[3])
> then return index
>
> So, if the user supplies a, it's matched, otherwise, it skips that
> match. So on for b and c. Here is my real Icon code:
>
> every idx := startAt to *spec do {
> if (/loopName | loopName == spec[idx][1]) &
> (/segName | segName == spec[idx][2]) &
> ( /segMod | segMod == spec[idx][3]) then return idx
> }
>
> When my function runs, I get this:
>
> Run-time error 103
> File edispec.icn; Line 14
> string expected
> offending value: &null
You're being bitten (probably) by backtracking. Suppose, for
discussion, that loopName is null, but segName is not. However
also assume that segName is *not* == to spec[idx][2]. So the evaluation
goes something like:
/loopName succeeds, leaving the alternative (loopName == spec[idx][1])
unevaluated.
/segName fails, forcing evaluation of the alternative
(segName==spec[idx][2])
which *also fails*
at this point, you'll backtrack to the unevaluated alternative
(loopName == spec[idx][1]). But, of course, since loopName is
null, you'll get the "offending value: &null" error.
You want to limit backtracking so you don't try alternatives for 'choices'
that have succeeded. You did that when you split it into separate if
expressions.
An alternative (sorry) is to explicitly limit backtracking with:
if (/loopName | loopName == spec[idx][1])\1 &
(/segName | segName == spec[idx][2])\1 &
(/segMod | segMod == spec[idx][3]) then return idx
(Note that you don't need to limit the last choice since you
won't backtrack into it. You could limit it if you wanted
visual symmetry, but it wouldn't affect the result.)
--
Steve Wampler -- swampler@[EMAIL PROTECTED]
gods that smiled on your birth are now laughing out loud.


|