0

I want to understand what and and or represent in Racket when used with lists. When I do something like this -

> (or `(1 2) `(1 3))
'(1 2)

What is the result representing? I thought when we use or with two lists we would get a union of the lists. That is clearly not what is happening here. So, I thought it is interpreted as boolean values and that is why `(1 2) is not a false value. Hence, the result is `(1 2). But what about this? -

> (and `(1 2) `(1 3))
'(1 3)

How can I justify this?

Saikumar
  • 3
  • 3
  • 2
    There are many "Racket" languages. Your examples show the standard [Scheme interpretation](https://docs.racket-lang.org/r5rs/r5rs-std/r5rs-Z-H-7.html#%25_idx_120), which is inherited by `#lang racket`, explained and justified in its [excellent documentation](https://docs.racket-lang.org/guide/conditionals.html#%28part._and%2Bor%29). Documentation can be accessed from DrRacket by right-clicking on a selection and choosing "Search in Help Desk for..." – mnemenaut Apr 12 '22 at 06:50

1 Answers1

3

or is looking for the first truthy value, checking arguments from left to right. Every value other than #false is truthy. If any is found, it's returned. In your example, '(1 2) is the first truthy value from the left, so it's returned.

Or called with no arguments returns #false, because no truthy values were found:

> (or)
#f

And checks whether all values are truthy, so it has to examine all of them, going from left to the right, and it can return #false (if any #false is found) or, if all values are truthy, last of them. In your example, '(1 3) is the last truthy value, so it's returned.

And called with no arguments returns #true, because no #false was found:

> (and)
#t

Read also the docs about or and and.

By the way, there is a difference between ' and `. First one is quote, the second one is quasiquote. In this very example, it doesn't matter, but you should know the difference between them. And if you were really looking for union function, check racket/set library:

(require racket/set)
(set-union (set 1 2) (set 1 3))
=> (set 1 3 2)
Martin Půda
  • 7,353
  • 2
  • 6
  • 13