Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Common Lisp: all or any elements are true in a list

In Python there are functions all and any they return true if all or some elements of a list are true respectively. Are there equivalent functions in Common Lisp? If not, what is the most succinct and idiomatic way to write them?

Currently i have this:

(defun all (xs)
  (reduce (lambda (x y) (and x y)) xs :initial-value t))

(defun any (xs)
  (reduce (lambda (x y) (or x y)) xs :initial-value nil))
like image 508
Mirzhan Irkegulov Avatar asked Dec 18 '12 19:12

Mirzhan Irkegulov


People also ask

Is everything a list in Lisp?

Lisp has one: everything is a list. The first element is a function name, and the rest are its arguments. Thus, the language is simply a collection of compile- and run-time functions, trivially extensible.

What does list do in Lisp?

The list function is rather used for creating lists in LISP. The list function can take any number of arguments and as it is a function, it evaluates its arguments. The first and rest functions give the first element and the rest part of a list. The following examples demonstrate the concepts.

What is true in Lisp?

In Lisp the convention is that nil, or the empty list, means false, and anything else means true. If you want to represent true you use the special Lisp operator, t.

What is a dot in a list in Lisp?

Lists are built up from smaller pieces in Lisp. The dot notation indicates those smaller pieces.


1 Answers

In Common Lisp, use every (that's the equivalent of all) and some (that's the equivalent of any).

like image 138
Óscar López Avatar answered Nov 02 '22 19:11

Óscar López