I am using Racket and Dr. Racket for an educational purpose.
After the following definitions of variable "x" and "y":
(define x (list 1 2 3))
(define y (list 4 5 6))
I decided to create 3 different lists using these variables.
First:
(append x y)
>> '(1 2 3 4 5 6)
Second:
(cons x y)
>> '((1 2 3) 4 5 6)
Third:
(list x y)
>> ((1 2 3) (4 5 6))
After that, I decided to use the boolean operators "and" and "or" with the three lists. What surprised me was the output. Why does this happen? Why "or" and "and" choose one of the lists? What's the ratio behind this decision?
(and (append x y) (cons x y) (list x y))
>> '((1 2 3) (4 5 6))
(or (append x y) (cons x y) (list x y))
>> '(1 2 3 4 5 6)
It's simple: and
returns the value of the last expression that is truthy or #f
if at least one expression is false, whereas or
returns the value of the first expression that is truthy or #f
if all are false.
Remember: in Scheme, the only false value is #f
, whereas anything else is considered true, hence we use the moniker "truthy" - to denote a non-false value. In particular, in your code this:
(and (append x y) (cons x y) (list x y))
Returns the value of the last truthy expression: (list x y)
, whereas this:
(or (append x y) (cons x y) (list x y))
Returns the value of the first truthy expression: (append x y)
.