Lisp - some Function



Lisp provides some function on sequence (list, vector or string) to check if a predicate holds t (true) for any of the elements of the sequence. some function applies the predicate on each element of the sequence in order. If any of element returns t (true) for the given predicate, some function calls stops and return t (true).

Syntax - some function

(some predicate sequence)

Arguments

  • predicate− predicate to be applied. It returns t if check on element of sequence is true otherwise nil.

  • sequence− sequence to be searched.

Returns

This function returns t (true) if predicate holds true for any of the elements of sequence, nil(false) otherwise.

Example - Checking if sequence contains any even number

Following example shows how to apply even number check on any element of the list and vectors.

main.lisp

; Check if any element of a list is even; T
(print(some #'evenp '(1 2 3 4)))
(terpri)
; Check if any elements of a vector is even; T
(print(some #'evenp #(1 2 3 4)))
(terpri)
; Check if any elements of a list is even; NIL, all numbers are odd
(print(some #'evenp '(1 3 5 7 9)))
(terpri)
; Check if any elements of a vector is even; NIL, all numbers are odd
(print(some #'evenp #(1 3 5 7 9)))

Output

When you execute the code, it returns the following result −

T
T
NIL
NIL

Example - Checking if any number greater than 10

Following example shows how to apply a custom function check using lambda function on every element of the list.

main.lisp

; Check if any numbers in the list is greater than 10; returns t as 15 is > 10
(print(some #'(lambda (x) (> x 10)) '(1 15 4 2)))  
(terpri)
; Check if any numbers in the list is greater than 10; returns NIL, as no number is > 10
(print(some #'(lambda (x) (> x 10)) '(1 5 7 2)))  

Output

When you execute the code, it returns the following result −

T
NIL

Example - Checking if String contains any of vowels

Following example shows how to apply predicate on string.

main.lisp

; Check if string contains any vowels; returns #u 
(print (some (lambda (char) (member char '(#\a #\e #\i #\o #\u))) "tutorialspoint")) 
(terpri)
; Check if string contains any vowels; returns NIL as no vowel is present
(print(some (lambda (char) (member char '(#\a #\e #\i #\o #\u))) "html")) 

Output

When you execute the code, it returns the following result −

(#\u) 
NIL 

Example - Checking Multiple lists

We can use some function on multiple sequences as well. The predicate must accept the same number of parameters as there are sequences as shown below:

main.lisp

; Checking if any element of first list is greater than corresponding element of second list
; return t as 3 > 2
(print(some #'> '(1 2 3) '(4 5 2)))
(terpri)
; Checking if any element of first list is greater than corresponding element of second list
; return NIL, as all elements of first list are less.
(print(some #'> '(1 2 3) '(4 5 6)))

Output

When you execute the code, it returns the following result −

T
NIL

Applications

The some function is very useful in scenarios where at least one element to meet a certain criteria. Following are few of the applications of some function.

  • Searching− To check if any of the entry is following the criteria.

  • Existence Check− To verify if at least one element is satisfying a condition.

  • Conditional Valuation− To trigger a collection, if any of the element is meeting a given criteria.

some is a concise way to express "exists" or "at least one" condition in sequene manipulation.

Advertisements