Re: pass by reference
- To: mathgroup at smc.vnet.net
- Subject: [mg93733] Re: [mg93685] pass by reference
- From: Sseziwa Mukasa <mukasa at jeol.com>
- Date: Mon, 24 Nov 2008 04:14:16 -0500 (EST)
- References: <200811221109.GAA20379@smc.vnet.net>
On Nov 22, 2008, at 6:09 AM, juan flores wrote: > Dear all, > > How do you pass by reference in Mathematica? You can't > > Let us say we want to program push and pop. > -------- > Push[el_, stack_] := Prepend[stack, el] > Pop[stack_] := {First[stack], Rest[stack]} > > stack = {1, 2, 3}; newel = 5; > > stack = Push[newel, stack] > {5, 1, 2, 3} > > {lastel, stack} = Pop[stack] > {5, {1, 2, 3}} > > stack > {1, 2, 3} > -------- > > Code is working, but it is kind of awkward to have to use pure > functional programming. What do Push and Pop add for clarity's sake given the existence of First, Rest etc.? Programming in an imperative style using functional constructs will always be awkward since you can't change the value of arguments to a function. > Any pointers/ideas will be greatly appreciated. Without more information about your problem it's hard to give much advice, but generally if you want to write a state machine functionally it's best expressed as functions that accept the current state, preferably contained in a single variable, and return the next state rather than modifying multiple variables. Hope that helps, Sseziwa Mukasa
- References:
- pass by reference
- From: juan flores <juanfie@gmail.com>
- pass by reference