Mathematica 9 is now available
Services & Resources / Wolfram Forums / MathGroup Archive
-----

MathGroup Archive 2009

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

Search the Archive

Strange behavior when using pattern match and HoldAll

  • To: mathgroup at smc.vnet.net
  • Subject: [mg104709] Strange behavior when using pattern match and HoldAll
  • From: dnquark <dnquark at gmail.com>
  • Date: Sat, 7 Nov 2009 06:50:13 -0500 (EST)

I am using HoldAll with a function that I would like to modify a list
passed to it, as described on
http://forums.wolfram.com/student-support/topics/21247
I wrote this:

Clear[test]; Remove[test];
SetAttributes[test, HoldAll]
test[list_List, val_] := list[[2]] = val;

This doesn't work:
lst = {4, 5, 6};
test[lst, 99];
lst

gives {4,5,6}

However, test[list_, val_] := list[[2]] = val  works.

Similarly,
Clear[foo]; Remove[foo];
SetAttributes[foo, HoldAll]
foo[list_List] := list[[2]];
lst={1,2,3}; foo[lst]

doesn't work.
Can someone explain why HoldAll changes the behavior of the function
depending on whether the argument pattern is specified as x_ or
x_List?..

Thanks,
--Leo


  • Prev by Date: Re: Wrong limit?
  • Next by Date: Re: Writing a graphic to a notebook
  • Previous by thread: Re: fitting
  • Next by thread: Re: Strange behavior when using pattern match and HoldAll