I have a head that holds a single argument, and I want to unpack the value inside it while also assigning a symbol to the value itself, for example:
f[y:g[x_]] := {y, x};
f[g[4]] (* == {g[4], 4} *)
This works fine, but when I want to give it a default argument, x does not bind to the inner value:
f[y:g[x_]:g[10]] := {y, x};
f[] (* == {g[10]}, rather than {g[10], 10} *)
How do I make x bind to the inside of the default value?
This is my real-world example:
squareLattice = lattice[{n1 -> {1, 0}, n2 -> {0, 1}}];
doRandomWalk[n_Integer, l:lattice[basis_]:squareLattice] :=
With[{basisSymbols = First /@ basis},
Echo[basis];
randomWalk[
Accumulate@
Table[RandomChoice[{1, -1}] RandomChoice[basisSymbols], n],
l]];
When I evaluate using the default argument: doRandomWalk[100]
, I get a message from Echo
, as basis
is not bound to anything
Thank you for any help!
ClearAll[f, g]; SetAttributes[f, HoldFirst];f[y : g[x_] : Sequence[g[10], 10]] := {y, x};
? $\endgroup$doRandomWalk[n_Integer, l_lattice: squareLattice] := Module[{basis = First[l], basisSymbols}, basisSymbols = First /@ basis; Echo[basis]; randomWalk[Accumulate @ Table[RandomChoice[{1, -1}] RandomChoice[basisSymbols], n], l]]
unsuitable for your needs? $\endgroup$lattice
should be just by looking at the pattern and not having to read the function body $\endgroup$