“Re-writing” the function `MemberQ`

The name of the pictureThe name of the pictureThe name of the pictureClash Royale CLAN TAG#URR8PPP











up vote
3
down vote

favorite












I am trying to program the function MemberQ using less then possible functions pre defined by Mathematica.
Until this now my code is:



meuMemberQ[f_,n_?NumericQ] /; (Length[Select[f, # == n &]] != 0):=True
meuMemberQ[f_,n_Symbol] /; (Length[Select[f, # == n &]] != 0) := True
meuMemberQ[f_,n_String] /; (Length[Select[f, # == n &]] != 0) := True
meuMemberQ[f_, n_] := False


There is a way to writing the same function but without using Select and Length?



Ps: It is just for exercise.







share|improve this question




















  • meuMemberQ2[f_, n_] := Intersection[f, n] === n or meuMemberQ3[f_,n_]:= SubsetQ[f, n]?
    – kglr
    Sep 4 at 23:02











  • So... the ideia is not use others functions. In this case, not using Intersection
    – Mateus
    Sep 4 at 23:04










  • Hint: loop through the list elements and test each with MatchQ. You could use AnyTrue for convenience, or simply Table for a very basic implementation.
    – Szabolcs
    Sep 5 at 8:12














up vote
3
down vote

favorite












I am trying to program the function MemberQ using less then possible functions pre defined by Mathematica.
Until this now my code is:



meuMemberQ[f_,n_?NumericQ] /; (Length[Select[f, # == n &]] != 0):=True
meuMemberQ[f_,n_Symbol] /; (Length[Select[f, # == n &]] != 0) := True
meuMemberQ[f_,n_String] /; (Length[Select[f, # == n &]] != 0) := True
meuMemberQ[f_, n_] := False


There is a way to writing the same function but without using Select and Length?



Ps: It is just for exercise.







share|improve this question




















  • meuMemberQ2[f_, n_] := Intersection[f, n] === n or meuMemberQ3[f_,n_]:= SubsetQ[f, n]?
    – kglr
    Sep 4 at 23:02











  • So... the ideia is not use others functions. In this case, not using Intersection
    – Mateus
    Sep 4 at 23:04










  • Hint: loop through the list elements and test each with MatchQ. You could use AnyTrue for convenience, or simply Table for a very basic implementation.
    – Szabolcs
    Sep 5 at 8:12












up vote
3
down vote

favorite









up vote
3
down vote

favorite











I am trying to program the function MemberQ using less then possible functions pre defined by Mathematica.
Until this now my code is:



meuMemberQ[f_,n_?NumericQ] /; (Length[Select[f, # == n &]] != 0):=True
meuMemberQ[f_,n_Symbol] /; (Length[Select[f, # == n &]] != 0) := True
meuMemberQ[f_,n_String] /; (Length[Select[f, # == n &]] != 0) := True
meuMemberQ[f_, n_] := False


There is a way to writing the same function but without using Select and Length?



Ps: It is just for exercise.







share|improve this question












I am trying to program the function MemberQ using less then possible functions pre defined by Mathematica.
Until this now my code is:



meuMemberQ[f_,n_?NumericQ] /; (Length[Select[f, # == n &]] != 0):=True
meuMemberQ[f_,n_Symbol] /; (Length[Select[f, # == n &]] != 0) := True
meuMemberQ[f_,n_String] /; (Length[Select[f, # == n &]] != 0) := True
meuMemberQ[f_, n_] := False


There is a way to writing the same function but without using Select and Length?



Ps: It is just for exercise.









share|improve this question











share|improve this question




share|improve this question










asked Sep 4 at 22:52









Mateus

37216




37216











  • meuMemberQ2[f_, n_] := Intersection[f, n] === n or meuMemberQ3[f_,n_]:= SubsetQ[f, n]?
    – kglr
    Sep 4 at 23:02











  • So... the ideia is not use others functions. In this case, not using Intersection
    – Mateus
    Sep 4 at 23:04










  • Hint: loop through the list elements and test each with MatchQ. You could use AnyTrue for convenience, or simply Table for a very basic implementation.
    – Szabolcs
    Sep 5 at 8:12
















  • meuMemberQ2[f_, n_] := Intersection[f, n] === n or meuMemberQ3[f_,n_]:= SubsetQ[f, n]?
    – kglr
    Sep 4 at 23:02











  • So... the ideia is not use others functions. In this case, not using Intersection
    – Mateus
    Sep 4 at 23:04










  • Hint: loop through the list elements and test each with MatchQ. You could use AnyTrue for convenience, or simply Table for a very basic implementation.
    – Szabolcs
    Sep 5 at 8:12















meuMemberQ2[f_, n_] := Intersection[f, n] === n or meuMemberQ3[f_,n_]:= SubsetQ[f, n]?
– kglr
Sep 4 at 23:02





meuMemberQ2[f_, n_] := Intersection[f, n] === n or meuMemberQ3[f_,n_]:= SubsetQ[f, n]?
– kglr
Sep 4 at 23:02













So... the ideia is not use others functions. In this case, not using Intersection
– Mateus
Sep 4 at 23:04




So... the ideia is not use others functions. In this case, not using Intersection
– Mateus
Sep 4 at 23:04












Hint: loop through the list elements and test each with MatchQ. You could use AnyTrue for convenience, or simply Table for a very basic implementation.
– Szabolcs
Sep 5 at 8:12




Hint: loop through the list elements and test each with MatchQ. You could use AnyTrue for convenience, or simply Table for a very basic implementation.
– Szabolcs
Sep 5 at 8:12










2 Answers
2






active

oldest

votes

















up vote
4
down vote



accepted










A recursive implementation:



meuMemberQ[n_, y___, n_] := True;
meuMemberQ[x_, y___, n_] := meuMemberQ[y, n];
meuMemberQ[, n_] := False;


This just cuts through the list step by step looking for an exact match of n_, and if it gets to an empty list it returns False.



It will run into the iteration limit eventually, but if you need to use it with such long lists, there are things that can be done (such as using MemberQ).






share|improve this answer



























    up vote
    4
    down vote













    A few alternatives:



    ClearAll[meuMemberQ2, meuMemberQ3, meuMemberQ4]

    meuMemberQ2[f_, n_] := Switch[n, Alternatives @@ f, True, _, False]
    meuMemberQ3[f_, n_] := n /. Alternatives@@f ->True, _:> False

    meuMemberQ4[OrderlessPatternSequence[n_,___], n_] := True
    meuMemberQ4[_,_]:=False





    share|improve this answer






















      Your Answer




      StackExchange.ifUsing("editor", function ()
      return StackExchange.using("mathjaxEditing", function ()
      StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
      StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
      );
      );
      , "mathjax-editing");

      StackExchange.ready(function()
      var channelOptions =
      tags: "".split(" "),
      id: "387"
      ;
      initTagRenderer("".split(" "), "".split(" "), channelOptions);

      StackExchange.using("externalEditor", function()
      // Have to fire editor after snippets, if snippets enabled
      if (StackExchange.settings.snippets.snippetsEnabled)
      StackExchange.using("snippets", function()
      createEditor();
      );

      else
      createEditor();

      );

      function createEditor()
      StackExchange.prepareEditor(
      heartbeatType: 'answer',
      convertImagesToLinks: false,
      noModals: false,
      showLowRepImageUploadWarning: true,
      reputationToPostImages: null,
      bindNavPrevention: true,
      postfix: "",
      onDemand: true,
      discardSelector: ".discard-answer"
      ,immediatelyShowMarkdownHelp:true
      );



      );













       

      draft saved


      draft discarded


















      StackExchange.ready(
      function ()
      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181232%2fre-writing-the-function-memberq%23new-answer', 'question_page');

      );

      Post as a guest






























      2 Answers
      2






      active

      oldest

      votes








      2 Answers
      2






      active

      oldest

      votes









      active

      oldest

      votes






      active

      oldest

      votes








      up vote
      4
      down vote



      accepted










      A recursive implementation:



      meuMemberQ[n_, y___, n_] := True;
      meuMemberQ[x_, y___, n_] := meuMemberQ[y, n];
      meuMemberQ[, n_] := False;


      This just cuts through the list step by step looking for an exact match of n_, and if it gets to an empty list it returns False.



      It will run into the iteration limit eventually, but if you need to use it with such long lists, there are things that can be done (such as using MemberQ).






      share|improve this answer
























        up vote
        4
        down vote



        accepted










        A recursive implementation:



        meuMemberQ[n_, y___, n_] := True;
        meuMemberQ[x_, y___, n_] := meuMemberQ[y, n];
        meuMemberQ[, n_] := False;


        This just cuts through the list step by step looking for an exact match of n_, and if it gets to an empty list it returns False.



        It will run into the iteration limit eventually, but if you need to use it with such long lists, there are things that can be done (such as using MemberQ).






        share|improve this answer






















          up vote
          4
          down vote



          accepted







          up vote
          4
          down vote



          accepted






          A recursive implementation:



          meuMemberQ[n_, y___, n_] := True;
          meuMemberQ[x_, y___, n_] := meuMemberQ[y, n];
          meuMemberQ[, n_] := False;


          This just cuts through the list step by step looking for an exact match of n_, and if it gets to an empty list it returns False.



          It will run into the iteration limit eventually, but if you need to use it with such long lists, there are things that can be done (such as using MemberQ).






          share|improve this answer












          A recursive implementation:



          meuMemberQ[n_, y___, n_] := True;
          meuMemberQ[x_, y___, n_] := meuMemberQ[y, n];
          meuMemberQ[, n_] := False;


          This just cuts through the list step by step looking for an exact match of n_, and if it gets to an empty list it returns False.



          It will run into the iteration limit eventually, but if you need to use it with such long lists, there are things that can be done (such as using MemberQ).







          share|improve this answer












          share|improve this answer



          share|improve this answer










          answered Sep 4 at 23:10









          eyorble

          4,2501624




          4,2501624




















              up vote
              4
              down vote













              A few alternatives:



              ClearAll[meuMemberQ2, meuMemberQ3, meuMemberQ4]

              meuMemberQ2[f_, n_] := Switch[n, Alternatives @@ f, True, _, False]
              meuMemberQ3[f_, n_] := n /. Alternatives@@f ->True, _:> False

              meuMemberQ4[OrderlessPatternSequence[n_,___], n_] := True
              meuMemberQ4[_,_]:=False





              share|improve this answer


























                up vote
                4
                down vote













                A few alternatives:



                ClearAll[meuMemberQ2, meuMemberQ3, meuMemberQ4]

                meuMemberQ2[f_, n_] := Switch[n, Alternatives @@ f, True, _, False]
                meuMemberQ3[f_, n_] := n /. Alternatives@@f ->True, _:> False

                meuMemberQ4[OrderlessPatternSequence[n_,___], n_] := True
                meuMemberQ4[_,_]:=False





                share|improve this answer
























                  up vote
                  4
                  down vote










                  up vote
                  4
                  down vote









                  A few alternatives:



                  ClearAll[meuMemberQ2, meuMemberQ3, meuMemberQ4]

                  meuMemberQ2[f_, n_] := Switch[n, Alternatives @@ f, True, _, False]
                  meuMemberQ3[f_, n_] := n /. Alternatives@@f ->True, _:> False

                  meuMemberQ4[OrderlessPatternSequence[n_,___], n_] := True
                  meuMemberQ4[_,_]:=False





                  share|improve this answer














                  A few alternatives:



                  ClearAll[meuMemberQ2, meuMemberQ3, meuMemberQ4]

                  meuMemberQ2[f_, n_] := Switch[n, Alternatives @@ f, True, _, False]
                  meuMemberQ3[f_, n_] := n /. Alternatives@@f ->True, _:> False

                  meuMemberQ4[OrderlessPatternSequence[n_,___], n_] := True
                  meuMemberQ4[_,_]:=False






                  share|improve this answer














                  share|improve this answer



                  share|improve this answer








                  edited Sep 4 at 23:30

























                  answered Sep 4 at 23:23









                  kglr

                  159k8183382




                  159k8183382



























                       

                      draft saved


                      draft discarded















































                       


                      draft saved


                      draft discarded














                      StackExchange.ready(
                      function ()
                      StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fmathematica.stackexchange.com%2fquestions%2f181232%2fre-writing-the-function-memberq%23new-answer', 'question_page');

                      );

                      Post as a guest













































































                      Comments

                      Popular posts from this blog

                      What does second last employer means? [closed]

                      List of Gilmore Girls characters

                      Confectionery