Services & Resources / Wolfram Forums
-----
 /
MathGroup Archive
2007
*January
*February
*March
*April
*May
*June
*July
*August
*September
*October
*Archive Index
*Ask about this page
*Print this page
*Give us feedback
*Sign up for the Wolfram Insider

MathGroup Archive 2007

[Date Index] [Thread Index] [Author Index]

Search the Archive

Re: Call-by-reference from inside a function

  • To: mathgroup at smc.vnet.net
  • Subject: [mg72580] Re: Call-by-reference from inside a function
  • From: albert <awnl at arcor.de>
  • Date: Wed, 10 Jan 2007 03:07:03 -0500 (EST)
  • References: <eo012n$kp1$1@smc.vnet.net>

Hi Istvan,

> I'm in need of a function, which is called with one argument, a list.
> The function is to chose one element of the list randomly, AND modify
> the original list by removing the chosen element from it. My problem is
> that I cannot modify the global variable from inside the function. It
> is not a solution to return with a list such as:
> Return[{chosen_element, modified_local_list}], and than make an
> assignment outside the function.
> I'm thinking on some call-by-reference method, but as I've learned so
> far, there is no user-friendly way to do this, just some workarounds
> (which I don't know). Any suggestions?

What you are probably looking for is the attribute HoldAll or HoldFirst. Is
the following doing what you want and user-friendly enough?

SetAttributes[RandomChoseElem, HoldFirst];

RandomChoseElem[list_?(MatchQ[#, _List] &)] := Module[{
        pos, elem
        },
      pos = Random[Integer, {1, Length[list]}];
      
      elem = list[[pos]];
      list = Delete[list, pos];
      
      Return[elem]
      ];

you probably want to define a reasonable return value for the empty list...

hth,

albert


  • Prev by Date: Export to xmgrace format
  • Next by Date: Re: Call-by-reference from inside a function
  • Previous by thread: Re: Call-by-reference from inside a function
  • Next by thread: Re: Call-by-reference from inside a function