Take the 2-minute tour ×
Mathematica Stack Exchange is a question and answer site for users of Mathematica. It's 100% free, no registration required.

In Mathematica 7, I want to determine whether a list of free of numbers greater than 1 or not. It seems like using FreeQ would be a concise way to do this:

FreeQ[{2, 1}, # > 1 &] (* The output is: True *)

FreeQ[{1, 1}, # > 1 &] (* The output is: True *)

Why does FreeQ[{2, 1}, # > 1 &] give True? {2, 1} is not free of numbers greater than 1, so I must be using FreeQ incorrectly.

share|improve this question
add comment

2 Answers 2

up vote 6 down vote accepted

FreeQ[] expects an object or a pattern as its second argument, as opposed to a Boolean function. What you should be doing is FreeQ[{2, 1}, _?(# > 1 &)]. Yes, the parentheses are needed.

A similar statement applies to the other pattern-matching functions of Mathematica, e.g. Cases[], Position[], and MatchQ[].

share|improve this answer
    
Ah, I made a silly mistake! Thank you for being patient and gentle with me. –  Andrew May 19 '12 at 18:21
add comment

Supporting J.M.'s answer you could also use Condition rather than PatternTest:

FreeQ[{2, 1}, x_ /; x > 1]

This is usually slightly faster. Faster still in many cases is Max, assuming numeric data:

Max[{2, 1}] <= 1
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

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