and is a short-circuiting logical conjunction special form on Boolean expressions.
Syntax
(and ⟨boolean-expr⟩*)
Semantics
- Every
⟨boolean-expr⟩must have typeBoolean, and theandexpression has typeBoolean. - Arguments are evaluated left-to-right:
- As soon as an argument evaluates to
False,andimmediately returnsFalsewithout evaluating any remaining arguments. - If all arguments evaluate to
True,andreturnsTrue. - With zero arguments,
(and)evaluates toTrue(the identity element of conjunction).
- As soon as an argument evaluates to
- The last argument of a non-empty
andform is in tail position when theandexpression is in tail position (including forrectail calls).
Examples
(coalton-toplevel
;; Short-circuiting prevents division by zero in the second test
(declare divides? (Integer * Integer -> Boolean))
(define (divides? d n)
(and (/= d 0)
(== (mod n d) 0))))
(coalton (Tuple (divides? 0 10) (divides? 5 10)))
; => #.(TUPLE COMMON-LISP:NIL COMMON-LISP:T)
Other notes
- Unlike Common Lisp’s
cl:and, which accepts arbitrary types as generalized booleans, Coalton’sandrequires every operand to have typeBoolean. - Because
andis a special form (to support short-circuiting), it cannot be passed as a first-class function value; useboolean-andwhen a function of type(Boolean * Boolean -> Boolean)is needed. - See
or.