Talk About Network

Google


Register and Login
Nick
Password
Register create new account Sign up is FREE and you can post replies, new topics, bookmark posts and more!
Recover lost password


Programming > Icon > Re: If statemen...
Latest [ Topics | Posts ] Archive Post A New Topic Post a Reply
<< Topic < Post Post 2 of 4 Topic 131 of 189
Post > Topic >>

Re: If statement

by Steve Wampler <swampler@[EMAIL PROTECTED] > Nov 27, 2006 at 09:22 AM

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.
 




 4 Posts in Topic:
If statement
Jeremy Cowgar <j.e.rem  2006-11-22 19:39:51 
Re: If statement
Steve Wampler <swample  2006-11-27 09:22:21 
Re: If statement
Cary Coutant <cary@[EM  2006-11-27 17:41:03 
Re: If statement
Steve Wampler <swample  2006-11-28 07:38:19 

Post A Reply:
  Go here to Signup

AddThis Feed Button


About - Advertising - Contact - Frequently Asked Questions - Privacy Policy - Terms of Use - Signup

Contact
tan12V112 Fri Jul 18 20:38:14 CDT 2008.