bill lam wrote:
>  IIRC ancient J had 'given name' that can be assigned once and can not
>  be changed afterwards.

We can get pretty close to this behavior in the current version of J.  I
just tossed together a pretty simple "Single Assigment J" script.  Get it
here:  http://www.jsoftware.com/svn/DanBron/trunk/environment/saj.ijs  .

Example usage:

        NB. Simple use:  define a name.
           G              NB.  Undefined
        |value error: G

           G is 22        NB.  Define it
        22

           G              NB.  Now it's 22.
        22

           G =: 55        NB.  But it can't be redefined
        |read-only data
        |   G    =:55
                

        NB.  Similarly for local assignment
           3 : ('L isL y';'L + 1') 8   
        9

           L                             NB.  L only available within local 
scope
        |value error: L

           3 : ('L isL y';'L =. 1') 8    NB.  Can't be redefined within local 
scope
        |read-only data
        |   L    =.1
        

        NB.  The adverbs try to protect you from overwriting previous 
assignments 
        NB.  which used the normal copulae
           avg  =: +/ % #
           avg is 45
        |domain error
        |       avg is 45


        NB.  And are even smart about allowing you to shadow globals with locals
        NB.  as you'd expect...
           3 : 'avg isL 45' ''
        45

           avg
        +/ % #
           
        NB.  But because they're just adverbs, and not meta-syntactic, they 
can't
        NB.  protect you from everything...
           msg =: 'hello'
           msg is 'goodbye'
        goodbye

           msg
        hello
           hello
        goodbye

           erase'msg hello'
           
        NB.  So when you want to be absolutely safe, use the quoted form:
           msg  =: 'hello'
           'msg' is 'goodbye'
        |domain error
        |       'msg'is'goodbye'
        |[-0] 
           
           msg
        hello
           hello
        |value error: hello
           
        
        NB.  The boxed form also works
           (<'msg') is 'goodbye'
        |domain error
        |       (<'msg')is'goodbye'
   
        NB.  But multiple assigments, evocations, etc, will almost certainly 
fail. 
        
        NB.  Also, while I believe the verbs should be entirely locale-safe
        NB.  (because I wrote them tacitly and used ". rather than some other
        NB.  mechanism), I haven't tested any cases.

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

Reply via email to