Contents    Page-10    Prev    Next    Page+10    Index   

Some and Every

The some function ( or there exists in math notation) applies a given function to each element of a list, returning the first result that is not nil or false:

(some function list )


(some (fn [x] (and (> x 3) x))
      '(1 2 17 4 0) )
    ->  17

Note that in this case we both tested for the desired item (> x 3) and then returned the item itself, since we want that answer rather than true as returned by the test.

The every? function ( or for all in math notation) applies a given function to each element of a list, returning true if the function is not nil or false for every list element.

(every? function list )


(every? (fn [x] (> x 3)) '(17 4 5))
    ->  true