Skip to main content
Commonmark migration
Source Link

I'm still thinking about how to implement Condition with anonymous patterns. And now I think I've found a way to implement it, but again, a suspected unclear documentation blocked me......

It's said in documentation that:

HoldPattern[expr] is equivalent to expr for pattern matching, but maintains expr in an unevaluated form.


But in this example, I simply adds a HoldPattern to the original okay pattern, then it generates a message and stop evaluation:

{{},{a,a},{a,b},{a,a,a}}/.{(Pattern[Evaluate[x=Unique[]],_]/;(Echo@x; True))..}->xxx

A seires of $--an integer-- is returned

{{},{a,a},{a,b},{a,a,a}}/.{HoldPattern[(Pattern[Evaluate[x=Unique[]],_]/;(Echo@x; True))]..}->xxx

Pattern: First element in pattern Pattern[Evaluate[x=Unique[]],_] is not a valid pattern name.


It seems that HoldPattern didn't really hold the pattern all the time, or else it won't say those Pattern stuff is invalid because when actually evaluating, the x=Unique[] will evaluate first then everything will be fine.

Is this yet another unclear explanation in documentation or it's my mis-implementation of HoldPattern?

And are there anyway I can solve this problem?

Thanks!


#Some further explanation

Some further explanation

I found out that though we cannot easily use anonymous pattern with Condition directly, we can create some single use symbol using Unique[] and then use them as names used in Condition. This will be identical as anonymous Condition, right~ But one crucial thing is to let Unique[] run again and again in multiple matches instead of calculating only once and leave there.

HoldPattern is designed for this job as it will surpress calculation in the first place and rerun each time pattern matcher want to check a match. But it seems that HoldPattern did something when nothing should be done, probably something like pre-processing, which result in this situation.

I'm still thinking about how to implement Condition with anonymous patterns. And now I think I've found a way to implement it, but again, a suspected unclear documentation blocked me......

It's said in documentation that:

HoldPattern[expr] is equivalent to expr for pattern matching, but maintains expr in an unevaluated form.


But in this example, I simply adds a HoldPattern to the original okay pattern, then it generates a message and stop evaluation:

{{},{a,a},{a,b},{a,a,a}}/.{(Pattern[Evaluate[x=Unique[]],_]/;(Echo@x; True))..}->xxx

A seires of $--an integer-- is returned

{{},{a,a},{a,b},{a,a,a}}/.{HoldPattern[(Pattern[Evaluate[x=Unique[]],_]/;(Echo@x; True))]..}->xxx

Pattern: First element in pattern Pattern[Evaluate[x=Unique[]],_] is not a valid pattern name.


It seems that HoldPattern didn't really hold the pattern all the time, or else it won't say those Pattern stuff is invalid because when actually evaluating, the x=Unique[] will evaluate first then everything will be fine.

Is this yet another unclear explanation in documentation or it's my mis-implementation of HoldPattern?

And are there anyway I can solve this problem?

Thanks!


#Some further explanation

I found out that though we cannot easily use anonymous pattern with Condition directly, we can create some single use symbol using Unique[] and then use them as names used in Condition. This will be identical as anonymous Condition, right~ But one crucial thing is to let Unique[] run again and again in multiple matches instead of calculating only once and leave there.

HoldPattern is designed for this job as it will surpress calculation in the first place and rerun each time pattern matcher want to check a match. But it seems that HoldPattern did something when nothing should be done, probably something like pre-processing, which result in this situation.

I'm still thinking about how to implement Condition with anonymous patterns. And now I think I've found a way to implement it, but again, a suspected unclear documentation blocked me......

It's said in documentation that:

HoldPattern[expr] is equivalent to expr for pattern matching, but maintains expr in an unevaluated form.


But in this example, I simply adds a HoldPattern to the original okay pattern, then it generates a message and stop evaluation:

{{},{a,a},{a,b},{a,a,a}}/.{(Pattern[Evaluate[x=Unique[]],_]/;(Echo@x; True))..}->xxx

A seires of $--an integer-- is returned

{{},{a,a},{a,b},{a,a,a}}/.{HoldPattern[(Pattern[Evaluate[x=Unique[]],_]/;(Echo@x; True))]..}->xxx

Pattern: First element in pattern Pattern[Evaluate[x=Unique[]],_] is not a valid pattern name.


It seems that HoldPattern didn't really hold the pattern all the time, or else it won't say those Pattern stuff is invalid because when actually evaluating, the x=Unique[] will evaluate first then everything will be fine.

Is this yet another unclear explanation in documentation or it's my mis-implementation of HoldPattern?

And are there anyway I can solve this problem?

Thanks!


Some further explanation

I found out that though we cannot easily use anonymous pattern with Condition directly, we can create some single use symbol using Unique[] and then use them as names used in Condition. This will be identical as anonymous Condition, right~ But one crucial thing is to let Unique[] run again and again in multiple matches instead of calculating only once and leave there.

HoldPattern is designed for this job as it will surpress calculation in the first place and rerun each time pattern matcher want to check a match. But it seems that HoldPattern did something when nothing should be done, probably something like pre-processing, which result in this situation.

edited title
Link

Wierd Weird behaviour of HoldPattern

Rollback to Revision 4
Source Link
Wjx
  • 9.7k
  • 1
  • 34
  • 70

I'm still thinking about how to implement Condition with anonymous patterns. And now I think I've found a way to implement it, but again, a suspected unclear documentation blocked me......

It's said in documentation that:

HoldPattern[expr] is equivalent to expr for pattern matching, but maintains expr in an unevaluated form.


But in this example, I simply adds a HoldPattern to the original okay pattern, then it generates a message and stop evaluation:

{{},{a,a},{a,b},{a,a,a}}/.{(Pattern[Evaluate[x=Unique[]],_]/;(Echo@x; True))..}->xxx

A seires of $--an integer-- is returned

{{},{a,a},{a,b},{a,a,a}}/.{HoldPattern[(Pattern[Evaluate[x=Unique[]],_]/;(Echo@x; True))]..}->xxx

Pattern: First element in pattern Pattern[Evaluate[x=Unique[]],_] is not a valid pattern name.


It seems that HoldPattern didn't really hold the pattern all the time, or else it won't say those Pattern stuff is invalid because when actually evaluating, the x=Unique[] will evaluate first then everything will be fine.

Is this yet another unclear explanation in documentation or it's my mis-implementation of HoldPattern?

And are there anyway I can solve this problem?

Thanks!


#Edit 1

Well, it seems that I


#Some further explanation

I found out that though we cannot easily use anonymous pattern with Condition directly, we can create some single use symbol using Unique[] and then use them as names used in Condition. This will be identical as anonymous Condition, right~ But one crucial thing is to let Unique[] run again and again in multiple matches instead of calculating only once and leave there.

HoldPattern is designed for this job as it will surpress calculation in the first place and rerun each time pattern matcher want to check a match. But it seems that HoldPattern did something when nothing should be done, probably something like pre-processing, which result in this situation.

I'm still thinking about how to implement Condition with anonymous patterns. And now I think I've found a way to implement it, but again, a suspected unclear documentation blocked me......

It's said in documentation that:

HoldPattern[expr] is equivalent to expr for pattern matching, but maintains expr in an unevaluated form.


But in this example, I simply adds a HoldPattern to the original okay pattern, then it generates a message and stop evaluation:

{{},{a,a},{a,b},{a,a,a}}/.{(Pattern[Evaluate[x=Unique[]],_]/;(Echo@x; True))..}->xxx

A seires of $--an integer-- is returned

{{},{a,a},{a,b},{a,a,a}}/.{HoldPattern[(Pattern[Evaluate[x=Unique[]],_]/;(Echo@x; True))]..}->xxx

Pattern: First element in pattern Pattern[Evaluate[x=Unique[]],_] is not a valid pattern name.


It seems that HoldPattern didn't really hold the pattern all the time, or else it won't say those Pattern stuff is invalid because when actually evaluating, the x=Unique[] will evaluate first then everything will be fine.

Is this yet another unclear explanation in documentation or it's my mis-implementation of HoldPattern?

And are there anyway I can solve this problem?

Thanks!


#Edit 1

Well, it seems that I


#Some further explanation

I found out that though we cannot easily use anonymous pattern with Condition directly, we can create some single use symbol using Unique[] and then use them as names used in Condition. This will be identical as anonymous Condition, right~ But one crucial thing is to let Unique[] run again and again in multiple matches instead of calculating only once and leave there.

HoldPattern is designed for this job as it will surpress calculation in the first place and rerun each time pattern matcher want to check a match. But it seems that HoldPattern did something when nothing should be done, probably something like pre-processing, which result in this situation.

I'm still thinking about how to implement Condition with anonymous patterns. And now I think I've found a way to implement it, but again, a suspected unclear documentation blocked me......

It's said in documentation that:

HoldPattern[expr] is equivalent to expr for pattern matching, but maintains expr in an unevaluated form.


But in this example, I simply adds a HoldPattern to the original okay pattern, then it generates a message and stop evaluation:

{{},{a,a},{a,b},{a,a,a}}/.{(Pattern[Evaluate[x=Unique[]],_]/;(Echo@x; True))..}->xxx

A seires of $--an integer-- is returned

{{},{a,a},{a,b},{a,a,a}}/.{HoldPattern[(Pattern[Evaluate[x=Unique[]],_]/;(Echo@x; True))]..}->xxx

Pattern: First element in pattern Pattern[Evaluate[x=Unique[]],_] is not a valid pattern name.


It seems that HoldPattern didn't really hold the pattern all the time, or else it won't say those Pattern stuff is invalid because when actually evaluating, the x=Unique[] will evaluate first then everything will be fine.

Is this yet another unclear explanation in documentation or it's my mis-implementation of HoldPattern?

And are there anyway I can solve this problem?

Thanks!


#Some further explanation

I found out that though we cannot easily use anonymous pattern with Condition directly, we can create some single use symbol using Unique[] and then use them as names used in Condition. This will be identical as anonymous Condition, right~ But one crucial thing is to let Unique[] run again and again in multiple matches instead of calculating only once and leave there.

HoldPattern is designed for this job as it will surpress calculation in the first place and rerun each time pattern matcher want to check a match. But it seems that HoldPattern did something when nothing should be done, probably something like pre-processing, which result in this situation.

added 46 characters in body
Source Link
Wjx
  • 9.7k
  • 1
  • 34
  • 70
Loading
added 524 characters in body
Source Link
Wjx
  • 9.7k
  • 1
  • 34
  • 70
Loading
added 524 characters in body
Source Link
Wjx
  • 9.7k
  • 1
  • 34
  • 70
Loading
added 7 characters in body
Source Link
Kuba
  • 137.7k
  • 13
  • 289
  • 751
Loading
Source Link
Wjx
  • 9.7k
  • 1
  • 34
  • 70
Loading