Sorry about the last message, I accidentally hit send.  My completed response 
follows:

Kairit wrote:
> But how to get rid of nested if-s?
> 
> If cond1 do
>       Something1
> Else
>       If cond2 do
>               Something2
>       Else
>               Something3
>       End
> End

           ni          =:  verb define
                  if. 2 | y do.
                          <:y
                  else.
                          if. y>100 do.
                                -: y
                          else.
                                y
                          end.
                  end.
        )
           
           cond1       =:  2&|
           Something1  =:  <:
           
           cond2       =:  >&100
           Something2  =:  -:
           Something3  =:  ]
           
           tni =: [EMAIL PROTECTED](cond1 (* >:)&:-. cond2)
           
           ni 3
        2
           ni 110
        55
           ni 48
        48
           
           tni 3
        2
           tni 110
        55
           tni 48
        48
            
>  Is there any rule of thumb for this case or is the approach  
>  everytime depending on the specifics of the case?

Sure:  change how you think ;)   Scalar languages, like C, focus on details; 
array languages, like J, focus on the "big picture".
So in C you make many small decisions, which manifest as nested conditions.  
You don't do that so much in J.

To answer your question a different way (which will be more useful immediately 
but may be detrimental in the long run):  If I were
converting a C program to J, I might re-write all the nested conditionals as 
select statements.  Then I would phrase those
statements in J using agenda  @.  , like I did above.

Some caveats:  

   *  Tacit code is driven entirely by its arguments.  So, if your 
      predicates rely on (mutable) global data, you cannot write
      a tacit equivalent.

   *  Sometimes conditionals are nested because the inner condition 
      should not be executed unless the outer condition is true 
      (or false).  For example:

        if.  noun=nc'variable' do.
                if. variable>100 do.
                        variable + 17
                end.
        end.

      Here, the second condition doesn't even make sense 
      syntactically unless the first condition is true.

      But, the way I wrote  tni  all the conditions 
      will be executed (i.e. the 1st condition being 
      false will not prohibit the 2nd from being tested).


   *  Along those lines:  in theory, given the way I wrote
      tni  all the tests are conducted simultaneously (in
      parallel).  So, for example:

        if.  y>10 do.
                y=.10|y
                if. y e. 1 3 5 do.
                   foo y
                end.
        end.    

      would have to be rewritten a different way (serially):

                foo^:(1 3 5 e.~ 10 | ])^:(>&10)

     This pattern can also be used to rewrite examples
     like the prior bullet's (i.e. to make execution of
     and "inner" condition dependent upon the result
     of an "outer" condition).

Does this answer your question?  If not, you could post a number of your "real 
life" nested conditions, and we can show you how
they'd be written in J.

-Dan

----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to