# \`and/or\` accept values that are not boolean

**URL:** <https://racket.discourse.group/t/and-or-accept-values-that-are-not-boolean/407>\
**Category:** Questions & Answers\
**Created:** [December 11, 2021, 9:04am UTC](https://racket.discourse.group/t/and-or-accept-values-that-are-not-boolean/407 "2021-12-11T09:04:53Z")\
**Posts on this page:** 5\
**Page:** 1

<div class="post-metadata">

**Author:** ![dannypsnl](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/dannypsnl/32/917_2.png) [@dannypsnl](https://racket.discourse.group/u/dannypsnl)\
**Post date:** [December 11, 2021, 9:04am UTC](https://racket.discourse.group/t/and-or-accept-values-that-are-not-boolean/407/1 "2021-12-11T09:04:53Z")

</div>

For example, we can write down

```scheme
(and #t 1 0) ; 0
(and 1 #t 0) ; 0
(and 1 0 #t) ; #t
(or 0 #t #f) ; 0
(or #t 0 #f) ; #t
(or #t #f 0) ; #t

```

I think these are two questions

1. why they accept non-boolean?
2. how should I interpret results in the mind model?

---

<div class="post-metadata">

**Author:** ![sorawee](https://avatars.discourse-cdn.com/v4/letter/s/ea5d25/32.png) [@sorawee](https://racket.discourse.group/u/sorawee)\
**Post date:** [December 11, 2021, 10:22am UTC](https://racket.discourse.group/t/and-or-accept-values-that-are-not-boolean/407/2 "2021-12-11T10:22:26Z")

</div>

[https://docs.racket-lang.org/guide/conditionals.html](https://docs.racket-lang.org/guide/conditionals.html) should be able to answer both of your questions.

---

<div class="post-metadata">

**Author:** ![dannypsnl](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/dannypsnl/32/917_2.png) [@dannypsnl](https://racket.discourse.group/u/dannypsnl)\
**Post date:** [December 11, 2021, 11:31am UTC](https://racket.discourse.group/t/and-or-accept-values-that-are-not-boolean/407/3 "2021-12-11T11:31:10Z")

</div>

emmm...yes, but still think it's weird XD.

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [December 11, 2021, 2:04pm UTC](https://racket.discourse.group/t/and-or-accept-values-that-are-not-boolean/407/4 "2021-12-11T14:04:36Z")

</div>

The key thing here is that `and` and `or` are not logical operators.

They are instead "short-circuting" operators, which means they don't evaluate all arguments if the result is clear from the first arguments.

The `or` operation returns the first "true" (non-false) value.

```scheme
> (or #f #t (/ 1 0))
#t

```

The evaluation was "short-circuited" in the sense that `(/ 1 0)` was never evaluated.

The second thing to keep in mind is that `#f` is the only false value and all other values count as true.  
This variation of the first example shows that `#t` can be replaced with any non-false value:

```scheme
> (or #f 42 (/ 1 0))
42

```

In short:

- The `or` operation returns the first "true" (non-false) value.
- If there are no "true" values, then the result is `#f`.
- Arguments after the first "true" are not evaluated.

Similarly:

- The `and` operation returns the last "true" value, if all arguments are "true".
- If at least one argument is false, the result is `#f`.
- If a false argument is found, the following arguments are not evaluated.

An common use of `and` is:

```scheme
(and <check-some-condition> <evaluate-expression>)

```

where the expression would result in an error, of the condition isn't met.

See more on short-circuiting here:

> **[Short-circuit evaluation](https://en.wikipedia.org/wiki/Short-circuit_evaluation)**
>
> Short-circuit evaluation, minimal evaluation, or McCarthy evaluation (after John McCarthy) is the semantics of some Boolean operators in some programming languages in which the second argument is executed or evaluated only if the first argument does not suffice to determine the value of the expression: when the first argument of the AND function evaluates to false, the overall value must be false; and when the first argument of the OR function evaluates to true, the overall value must be true.
> I...

---

<div class="post-metadata">

**Author:** ![sschwarzer](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/sschwarzer/32/1940_2.png) [@sschwarzer](https://racket.discourse.group/u/sschwarzer)\
**Post date:** [December 11, 2021, 2:34pm UTC](https://racket.discourse.group/t/and-or-accept-values-that-are-not-boolean/407/5 "2021-12-11T14:34:52Z")

</div>

One special case of

```scheme
(and <check-some-condition> <evaluate-expression>)

```

that I find interesting is using

```scheme
(and <some-value> #t)

```

to "convert" any "true" value to `#t`. In other words, with this expression the result will be either `#t` or `#f` (the latter if `<some-value>` is `#f`).
