2

I believe this is simple but I couldn't figure out why this doesn't work. Everything looks good to me.

DeleteCases[{{0, 0, 0, 0, 0, 0, x, 1, -1}, {0, 0, 0, 1, 0, 0, x, 
   1, -1}}, _?(#[[1 ;; 6]] == {0, 0, 0, 0, 0, 0}) &]
xzczd
  • 65,995
  • 9
  • 163
  • 468
emnha
  • 2,101
  • 13
  • 26
  • 2
    Because Head[(_?(#[[1 ;; 6]] == {0, 0, 0, 0, 0, 0}) &)] is Function not Pattern. Use this instead: x_ /; (x[[1 ;; 6]] == {0, 0, 0, 0, 0, 0}) – flinty Jun 13 '21 at 17:02
  • 4
    Or this, which may be what you had in mind: _?(#[[1 ;; 6]] == {0, 0, 0, 0, 0, 0} &) – Michael E2 Jun 13 '21 at 17:04
  • @flinty I checked the one below as to make it. Aren't they similar? https://mathematica.stackexchange.com/questions/197423/using-deletecases-with-a-defined-function-with-two-arguments-as-a-pattern – emnha Jun 13 '21 at 17:05
  • @MichaelE2 exactly! it is a subtle one – emnha Jun 13 '21 at 17:06
  • I still wonder why they are different? How the parentheses affect the result? – emnha Jun 13 '21 at 17:09
  • Ah, reading flinty again I think I get it. – emnha Jun 13 '21 at 17:10
  • 3
    The precedence of ? and &. The code in the question is equivalent to ( _?(#[[1 ;; 6]] == {0, 0, 0, 0, 0, 0}) ) &. In general _? body & is equivalent to (_? body) &. You need _?(body &). – Michael E2 Jun 13 '21 at 17:13
  • 3
    When you're not sure about the precedence, just click n times on the code, and the code piece will be selected outwards according to the precedence. – xzczd Jun 13 '21 at 17:17
  • @bbgodfrey I guess so. It's more abstract though. – emnha Jun 14 '21 at 00:48

1 Answers1

5

As mentioned in the comments, it's a matter of precedence. When you're not sure about the precedence, just click n times on the code, and the code piece will be selected outwards according to the precedence:

enter image description here

As shown in the GIF above, after clicking on & 3 times, _?(#[[1 ;; 6]] == {0, 0, 0, 0, 0, 0}) & is selected i.e. the whole _?(#[[1 ;; 6]] == {0, 0, 0, 0, 0, 0}) has been included in the Function, which is undesired.

Alternatively, you can check the FullForm. It's not that handy, but still a good choice if you're not yet familiar with the real meaning of _, ?, &, etc.

_?(#[[1 ;; 6]] == {0, 0, 0, 0, 0, 0} &) // FullForm
(*
PatternTest[Blank[],Function[Equal[Part[Slot[1],Span[1,6]],List[0,0,0,0,0,0]]]]
 *)

_?(#[[1 ;; 6]] == {0, 0, 0, 0, 0, 0}) & // FullForm (* Function[PatternTest[Blank[],Equal[Part[Slot[1],Span[1,6]],List[0,0,0,0,0,0]]]] *)

xzczd
  • 65,995
  • 9
  • 163
  • 468