i'm not requiring that a hammer always be used. i didn't ask if it was possible to calculate symSoc from society in J. i know you can do that without pattern matching.
i only wanted to know if J can do pattern matching. i obviously should have given a better example (but it's not a fair comparison if i use an input of society and you write code starting with p instead). but it gets too hard to explain (e.g. i'd like to see how J can be used to do write a program using the equivalent of the SocNetfunction which is very useful for doing agent-based modeling as indicated in my lecture notes). your solution is terrific, no question. but there care cases where using a scapel doesn't work or is inefficient. the "law of the hammer" could be called the "law of the scapel" just as well. there are cases where pattern matching is the appropriate or easiest tool and i want to know if J can do that. On Tue, Oct 8, 2013 at 4:12 PM, Roger Hui <[email protected]> wrote: > You have a hammer (pattern matching) and you are requiring that a problem > must be solved using a hammer. I solved the same problem with a scalpel. > > > > > On Tue, Oct 8, 2013 at 1:49 PM, Richard Gaylord <[email protected]> > wrote: > > > *sorry it took me so long to return to the thread. i've been dealing with > > health issues again (my advice is not to develop an autoimmune > neurological > > disease).* > > * > > * > > *the responses to my question about pattern matching in J really do not > > address the matter. the code given by roger* > > * > > * > > > > *p,|."1 p > > * > > * /:~p,|."1 p * > > * > > * > > *is very clever but it does not, as best i can tell, directly employ > > pattern matching while my WL code does. * > > * > > * > > *also, it does not start at the same point (use the same input) as the WL > > code, nor does it end at the same point (with the same output) as te WL > > code. * > > * > > * > > *perhaps it is my fault becuase of the specific example i used but let me > > restate it exactly as the corresponding code needs to appear and tn > perhaps > > the code to accomplish the same task in J can be posted * > > * > > * > > *(but it must have the identical Input [1] and Output [2] as the WL code > > (starting with p - which is not the same as society - is not acceptable > > (although i can certainly convert society into p in WL). it must start > > with society (or whatever you want to call it as long as it has the > > identical structure)* > > * > > * > > *Input[1]* > > * > > * > > *society = {{1, {5, 7}}, {2, {}}, {3, {}}, {4, {1, 2}}, {5, {2, 7}}, {6, > > {2}}, {7, {8}}, {8, {5}}};* > > * > > * > > *note: becuase of my use of the semi-colon, Input [1] does not return any > > Output* > > * > > * > > > > *note: the 2nd element in each list in society is not necessarily limited > > to an empty list and a 2-element list. it just happened to be that way.* > > * > > * > > > > *Input [2]* > > > > * > > * > > > > *symSoc = society /.* > > > > * {a_Integer, b_} :> {a, Union[b, Cases[society, {x_, {___, a, ___}} :> > > x]]}* > > > > * > > * > > > > *Output [2]* > > > > * > > * > > > > *{{1, {4, 5, 7}}, {2, {4, 5, 6}}, {3, {}}, {4, {1, 2}}, {5, {1, 2, 7, > > 8}}, {6, {2}}, {7, {1, 5, 8}}, {8, {5, 7}}}* > > > > * > > * > > > > > > *as for roger's question as to what the WL code is doing:* > > > > * > > * > > > > *we start in society with a list which contains a list * > > > > *for each person in the system (so if there are 8 people in society, the > > list consists of 8 elements, each a list.* > > > > * > > * > > > > *e.g.. for person A we have **{A, {B,**R,E}} **the first element is the > > person's name and the second element in each list is a list of the names > of > > the friends of A (let's say they are B, R, E).* > > > > * * > > > > *using symSoc, we look at the list of each and every person in the > system. > > specifically, we look at the 2nd element for each person to see if the > name > > A appears anywhere in another person's list of friends e.g. look at the > > list of friends of person D, and if A is in the list, then the name D is > > added to the list of friends of A. * > > > > * > > * > > > > *since it may be that D is already listed as a friend of A, D would > appear > > twice in the list of of A's friends so we use the Union command to set > rid > > of the duplicates of names and sort them as well.* > > > > * > > * > > > > *i hope this explanation is clear (i don't usually explain what what my > > code is doing; some of the functions i like to create are nested > anonymous > > (or pure) functions that are virtually unreadable, even to me a day or so > > after i've created them).* > > > > * > > * > > > > *finally, let me note that anything that can be done in J can be done in > WL > > and in this case, roger took my example and without using what i call > > pattern matching accomplished the same thing. but there are cases where > > pattern matching is, if not the only way to go, it is much easier to > write > > the code for.* > > > > * > > * > > > > *i hope this explains what's going on and what i' m asking about. if you > go > > to my note set (available as a pdf at > > http://library.wolfram.com/infocenter/MathSource/5216/ you'll find a > > tutorial on WL and the last section includes the code above and more code > > (i especially like the SocNetFunction i created which is a nested > anonymous > > function* > > > > * > > * > > > > *socNetFunction[rule_] := Function[y, Map[rule[Join[{#}, y[[#[[2]] ]] ] ] > > &, y] ]* > > > > * > > * > > > > * > > * > > > > * * > > > > > > > > > > -- > > > > *"Those who would sacrifice freedom for security deserve neither." - > > Benjamin Franklin* > > > > > > *"I think that the very notion that equations are a good approach to > > describing the natural world is a little bizarre." > > - Stephen Wolfram* > > > > * > > * > > > > nested > > ---------------------------------------------------------------------- > > For information about J forums see http://www.jsoftware.com/forums.htm > > > ---------------------------------------------------------------------- > For information about J forums see http://www.jsoftware.com/forums.htm > -- *"Those who would sacrifice freedom for security deserve neither." - Benjamin Franklin* *"I think that the very notion that equations are a good approach to describing the natural world is a little bizarre." - Stephen Wolfram* * * ---------------------------------------------------------------------- For information about J forums see http://www.jsoftware.com/forums.htm
