Corece to boolean in Common Lisp
Asked Answered
V

2

5

Is there a build-in function in Common Lisp to coerce any thing into it's boolean value, like the bool function in Python does?

Venture answered 29/10, 2018 at 15:9 Comment(0)
C
8

There's nothing specifically for this. You can't use boolean as the type argument to coerce. You can use:

(defun boolean-value (x)
  (not (not x)))

This is analogous to the !!x idiom used in many other languages.

Cheree answered 29/10, 2018 at 15:33 Comment(4)
I've seen the equivalent (not (null x)) more often as idiom, but both work.Christly
Me, too, I just didn't want to open up the "what's the difference between NOT and NULL" can of worms.Cheree
... so what is the difference?Luedtke
@Luedtke There's no operational difference. The difference to programmers is that NOT is for logical testing, while NULL is for structural testing.Cheree
P
3

Sort of. (and form t) will return t if form is not one of the false values nil or ().

I.e. it is a macro, not a function, and it requires the additional argument t to do the trick.

Prehension answered 29/10, 2018 at 15:31 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.