Google Mail Calendar Documents Reader Web more »
Recently Visited Groups | Help | Sign in
Google Groups Home
Strange behavior when using pattern match and HoldAll
There are currently too many topics in this group that display first. To make this topic appear first, remove this option from another topic.
There was an error processing your request. Please try again.
flag
  3 messages - Collapse all  -  Translate all to Translated (View all originals)
The group you are posting to is a Usenet group. Messages posted to this group will make your email address visible to anyone on the Internet.
Your reply message has not been sent.
Your post was successful
 
From:
To:
Cc:
Follow-up To:
Add Cc | Add Follow-up to | Edit Subject
Subject:
Validation:
For verification purposes please type the characters you see in the picture below or the numbers you hear by clicking the accessibility icon. Listen and type the numbers that you hear
 
dnquark  
View profile   Translate to Translated (View Original)
 More options 7 Nov, 11:49
Newsgroups: comp.soft-sys.math.mathematica
From: dnquark <dnqu...@gmail.com>
Date: Sat, 7 Nov 2009 11:49:37 +0000 (UTC)
Local: Sat 7 Nov 2009 11:49
Subject: Strange behavior when using pattern match and HoldAll
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


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Leonid Shifrin  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 11:50
Newsgroups: comp.soft-sys.math.mathematica
From: Leonid Shifrin <lsh...@gmail.com>
Date: Sun, 8 Nov 2009 11:50:42 +0000 (UTC)
Local: Sun 8 Nov 2009 11:50
Subject: Re: Strange behavior when using pattern match and HoldAll
Leo,

Hold-attributes do affect the pattern-matching. Have a look at

http://www.mathprogramming-intro.org/book/node408.html

where I specifically discuss this topic using an example similar to yours.

Regards,
Leonid


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
Albert Retey  
View profile   Translate to Translated (View Original)
 More options 8 Nov, 12:27
Newsgroups: comp.soft-sys.math.mathematica
From: Albert Retey <a...@gmx-topmail.de>
Date: Sun, 8 Nov 2009 12:27:20 +0000 (UTC)
Local: Sun 8 Nov 2009 12:27
Subject: Re: Strange behavior when using pattern match and HoldAll
dnquark schrieb:

Because of the HoldAll attribute the argument is the symbol lst, not
it's value, the List {1,2,3}. This is why it doesn't match the pattern
_List but _Symbol and thus the function body is never executed. You can
check that this works:

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

If you want to test whether the value of the symbol is a list, you could
do something like this:

test[list_Symbol?(Head[#] == List &),val_]:= list[[2]]=val;

or:

test[list_Symbol /; Head[list] === List, val_] := list[[2]] = val;

hth,

albert


    Reply    Reply to author    Forward  
You must Sign in before you can post messages.
To post a message, you must first join this group.
Please update your nickname on the subscription settings page before posting.
You do not have the permission required to post.
End of messages
« Back to Discussions « Newer topic     Older topic »

Create a group - Google Groups - Google Home - Terms of Service - Privacy Policy
©2009 Google