Occasionally I run into a Pattern that looks like i:func[arg_]:=i= an expression using arg
I have two questions on this:
- Can someone explain what this
Patterndoes? I do not understand the purpose of (if that is what is happening!) the subsequent application of setting thePatterni as theFunctionfunc which then "redefines" i viaSetDelayedand then some memoization (?) seems to be applied to i viaSet. - How could I have found this out myself? I could not get a clear understanding from
Traceor inspectingfunc//DownValuesneither did I find it in thePattern[]entry in the documentation orPatternrelated tutorials.
An example is given in peg solitaire. The Pattern used is:
i:findMoves[tab_]:=i=Flatten[#, 1]&[findMovesZero[tab, #]&/@Position[tab, 0]]
Another - more compact - one in an answer on: Using Memoization with a Mutable Object.
t : treeInsert[tree_, elem_] /; ! FreeQ[tree, elem] := t = tree
:, then press F1 (or whatever the help key is on your system). This shows in the documentation thats : objrepresents the patternobjwith the namesassigned to it. – Jack LaVigne Jun 22 '16 at 02:06:represents aPattern(I actually refer to it in my question). However this seems more complicated as here the pattern is theFunctionfunc which subsequently again "defines"iviaSetDelayedand then some memoization is applied to it viaSet. At least that's what I see, which I doubt I understand correctly. – Sander Jun 22 '16 at 03:08name:function[arg_]:=name=(do something with arg)is the same effect asfunction[arg_]:=function[arg]=(do something with arg)- it accomplishes "memoization" by dynamically defining a pattern with the computed result. The effect is documented in the tutorial "functions that remember their results" (or something like that), I don't recall if they show/use the first form there. – ciao Jun 22 '16 at 04:17mem :) and it was still not clear? If so I should rewrite that section for improved clarity. – Mr.Wizard Jul 19 '16 at 07:59