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.
meuMemberQ2[f_, n_] := Intersection[f, {n}] === {n}
ormeuMemberQ3[f_,n_]:= SubsetQ[f, {n}]
? $\endgroup$Intersection
$\endgroup$MatchQ
. You could useAnyTrue
for convenience, or simplyTable
for a very basic implementation. $\endgroup$