Lisp - Logical Operators

I'm new to Lisp, so it might be that simple, but I'm curious to know about it anyway.

I am familiar with logical operators such as AND and OR, but Lisp is not working properly.

For example, for (and 1 8)

Expected:

 1 => 0 0 0 1 8 => 1 0 0 0 (and 1 8) => 0 0 0 0 

Received: So, the answer was to be 0 ... but instead it is 8

Questions:

  • How is this calculation done in LISP?
  • Logical operators fundamentally different in LISP?
+5
source share
2 answers

In Common Lisp, AND and OR work with Boolean values, not binary digits. NIL is the only false value; everything else is considered true.

To work with binary representation of numbers, use LOGAND , LOGIOR , etc. All of them are described in http://clhs.lisp.se/Body/f_logand.htm .

 (logand 1 8) ==> 0 
+8
source

In programming languages, two types of and and or operators are often used. Conditional operators are called && and || in Algol languages, and in Common Lisp they are called and and or . On the other hand, the arithmetic operators & and | have the CL equivalents of logand and logior .

In Common Lisp, every value has a booleans value, and with the exception of nil every other value is considered a true value. Perl is very similar, except that it has a couple of false values, however 1 and 8 are true values ​​in both languages:

 1 && 8 # ==> 8 1 & 8 # ==> 0 1 || 8 # ==> 1 1 | 8 # ==> 9 

Same thing in CL

 (and 1 8) ; ==> 8 (logand 1 8) ; ==> 0 (or 1 8) ; ==> 1 (logior 1 8) ; ==> 9 
+5
source

Source: https://habr.com/ru/post/1266662/


All Articles