Search code examples
intbooleanlispcommon-lisp

Is there any way to parse from boolean to integer in Common Lisp?


I'm looking for some built-in function or some operator in Common Lisp that will return '0' when the input is a false boolean expression, and '1' when it is true. An example would be:

(setq a 3)
(setq b (+ (bool-to-int (< 0 a)) 2)
(print "b should be 1 + 2 = 3")

Is there a way to do this with Common Lisp without defining a custom function?


Solution

  • You can write (if <exp> 1 0) wherever you need to boolean expression to be mapped to [0, 1]. There is no builtin form to do it exactly as you wrote.