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))
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.
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.
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.
Lists are built up from smaller pieces in Lisp. The dot notation indicates those smaller pieces.
In Common Lisp, use every (that's the equivalent of all
) and some (that's the equivalent of any
).
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With