2
$\begingroup$

I have a difficulty to define the pattern to obtain this result:

Function[TypedData[{{1,2}, 2, {2,3,4}}] = True 

Function[TypedData[{{1,2}, 2, {2,3,4}}], TypedData[{{a, 1}, d, 1}]]= True

Here what I m trying :

Function[TypedData[{_list,_,_list}]__] := True

I put a BlankSequence after my TypedData because I wanted my function answers True if I have 1 or several TypedData but I doesn’t work.

May you help me to define a good pattern which can work with the TypedData ?

Thank you for your help.

$\endgroup$
6
  • $\begingroup$ Have you tried it with List instead of list? Also you probably want .. at the end of the pattern instead of __. And the blank in the middle insists on a middle expression, so three expressions inside TypedData, with lists at the beginning and end. Is that what you want? $\endgroup$
    – Michael E2
    Aug 8, 2021 at 15:02
  • $\begingroup$ i think you are completely right about the .. but i need also a blanksequence to ask Mathematica that i can have 1 or several typed Data. Indeed, Typed data is composed of list of 3 expressions $\endgroup$
    – Bendesarts
    Aug 8, 2021 at 15:16
  • $\begingroup$ @Michael E2 OK. it woks but i didn't understand why ".." can replace "__". How i have been able to do if have wanted 0, 1 or several typed Data in my case ? $\endgroup$
    – Bendesarts
    Aug 8, 2021 at 15:20
  • $\begingroup$ Function[TypedData[{_List, _, _List}]..] := True may be the answer. It seems to be a mistake that in your original expression there is only a left brace. It's a syntax error. $\endgroup$
    – RungeC
    Aug 8, 2021 at 15:26
  • $\begingroup$ @MichaelE2 a remind so that you can see my question. Thank you for your help. $\endgroup$
    – Bendesarts
    Aug 8, 2021 at 15:26

1 Answer 1

5
$\begingroup$
ClearAll["Global`*"]

Note that it is generally not a good idea to begin user-defined symbols with a capital letter since this could give rise to naming conflicts with built-in names. Perhaps what you intend is

typedDataQ[{_List, _Integer, _List} ..] := True

Note that if the middle argument is not intended to be an Integer just use _ or perhaps some other Head such as Real or Rational or String

Then to handle all other arguments

typedDataQ[___] := False

Testing valid data

typedDataQ[{{1, 2}, 2, {2, 3, 4}}]

(* True *)

Testing invalid data

typedDataQ[{3, 2, {2, 3, 4}}]

(* False *)

Testing multiple valid data

typedDataQ[{{1, 2}, 2, {2, 3, 4}}, {{6, 9, 6, 8}, 2, {1, 8, 2}}]

(* True *)

Testing mix of valid and invalid data

typedDataQ[{{1, 2}, 2, {2, 3, 4}}, {{6, 9, 6, 8}, 2, 7}]

(* False *)

Testing mix, item-by-item

typedDataQ /@ {{{1, 2}, 2, {2, 3, 4}}, {{6, 9, 6, 8}, 2, 7}}

(* {True, False} *)
$\endgroup$
5
  • $\begingroup$ Thank you ! You perfectly answer to my question $\endgroup$
    – Bendesarts
    Aug 8, 2021 at 15:39
  • $\begingroup$ Note also that _List covers a wide variety of structures. Depending on the application, you might want a tighter restriction such as _?VectorQ $\endgroup$
    – Bob Hanlon
    Aug 8, 2021 at 16:01
  • $\begingroup$ in fact it was not what I expected. I answered to quickly. I wanted the repeated .. after the TypedData and that is to say after the closing bracket $\endgroup$
    – Bendesarts
    Aug 8, 2021 at 19:28
  • $\begingroup$ may you have a look at my comment ? $\endgroup$
    – Bendesarts
    Aug 8, 2021 at 19:29
  • $\begingroup$ Your comment makes no sense to me. Don't tell us how to code, tell us how you want the code to perform. Provide examples of input for which the provided code does not do as you expect and tell us the result that you want instead. I will be away from my computer for 8 or 9 hours. $\endgroup$
    – Bob Hanlon
    Aug 8, 2021 at 19:42

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.