You could use symbols and names.
Then adding rows and columns won't break these references.
 
G2=: s:G1 =: ;:;._2 (0 : 0)
  Brit Sweede Dane German Norwegian
  Red Green White Yellow Blue
  Dogs Birds Cats Horses Fish
  Tea Coffee Milk Beer Water
  PallMall Dunhill Blends BlueMaster Prince
)

'Nation Color Pet Drink Smoke'=: G2

   (I.Nation=Sweede) , (I.Color=White)
1 2


----- Original Message ----
From: Sashikanth Chandrasekaran <[EMAIL PROTECTED]>
To: [email protected]
Sent: Wednesday, July 19, 2006 4:32:12 PM
Subject: [Jprogramming] guidelines for writing J code that is less-dependent on 
data layout


Assume that there are N entities each of which has M
attributes and I save the values as an N*M array. If I
want to work on the first attribute, it is tempting to
use {."1 . Similarly, if I want to work on the 3rd
attribute it is tempting to use 2{"1 and so on. But
this makes the program dependent on the data layout -
if I add one more attribute in the middle of the row,
the program breaks.

Are there any best practices/style guidelines to make
the code we write less dependent on data layout
without sacrificing performance? I presume that
"hiding" data inside a class and using trivial get/put
functions to access the data is not the J style.

To use a concrete example, I coded a simple solution
for the so-called Einstein riddle
(http://www.naute.com/puzzles/puzzle13.phtml) below.
As you can see the code is completely tied to the
location of the possible values in the grid - i.e. the
code will break if I switch 'Red' and 'Green' or
switch two rows etc. What is the recommended method
for making it less fragile? Thx, -sashi.


NB. Brute-force solution for Einstein's Riddle
grid =. ".;._2 (0 : 0)
  'Brit';'Sweede';'Dane';'German';'Norwegian'
  'Red';'Green';'White';'Yellow';'Blue'
  'Dogs';'Birds';'Cats';'Horses';'Fish'
  'Tea';'Coffee';'Milk';'Beer';'Water'
  'Pall Mall';'Dunhill';'Blends';'Blue
Master';'Prince'
)

NB. Table of permutations, copied from J vocabulary
page for A..
tap =. [EMAIL PROTECTED] A. i.

NB. The Brit lives in a red house. 
h =. (#~ (([: i.&0 {:) = [: i.&0 {.)"2) ,/ ,:"1"1 _~
tap 5

NB. The Swede keeps dogs as pets.
h =. ,/ h ,"_ 1"2 (([: ~:&1 {.)"2 h) #^:_1"1"1 _ >:
tap 4

NB. The Dane drinks tea.
h =. ,/ h ,"_ 1"2 (([: ~:&2 {.)"2 h) #^:_1"1"1 _ >:
tap 4

NB. The Green house is next to, and on the left of the
White house. 
h =. (#~ ([: (1: = i.&2 - i.&1) 1&{)"2) h

NB. The owner of the Green house drinks coffee. 
h =. (#~ (([: i.&1 {:) = [: i.&1 (1&{))"2) h

NB. The person who smokes Pall Mall rears birds. 
h =. ,/ h ,"_ 1"2 (([: ~:&1 (2&{))"2 h) #^:_1"1"1 _ >:
tap 4

NB. The owner of the Yellow house smokes Dunhill. 
h =. (#~ (([: i.&1 {:) = [: i.&3 (1&{))"2) h

NB. The man living in the centre house drinks milk. 
h =. (#~ (2: = (<3 2)&{)"2) h

NB. The Norwegian lives in the first house. 
h =. (#~ (4: = (<0 0)&{)"2) h

NB. The man who smokes Blends lives next to the one
who keeps cats. 
h =. (#~ ([: e.&_1 1 ([: i.&2 (2&{)) - [: i.&2 {:)"2)
h

NB. The man who keeps horses lives next to the man who
smokes Dunhill. 
h =. (#~ ([: e.&_1 1 ([: i.&1 {:) - [: i.&3 (2&{))"2)
h

NB. The man who smokes Blue Master drinks beer. 
h =. (#~ (([: i.&3 (3&{)) = [: i.&3 {:)"2) h

NB. The German smokes Prince. 
h =. (#~ (([: i.&4 {:) = [: i.&3 {.)"2) h

NB. The Norwegian lives next to the blue house. 
h =. (#~ ([: e.&_1 1 ([: i.&4 (1&{)) - [: i.&4 {.)"2)
h

NB. The man who smokes Blends has a neighbour who
drinks water.
h =. (#~ ([: e.&_1 1 ([: i.&4 (3&{)) - [: i.&2 {:)"2)
h

NB. display the valid combinations
h {"1"_1 _ grid 






__________________________________________________
Do You Yahoo!?
Tired of spam?  Yahoo! Mail has the best spam protection around 
http://mail.yahoo.com 
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm
----------------------------------------------------------------------
For information about J forums see http://www.jsoftware.com/forums.htm

Reply via email to