Corey
Corey

Reputation: 781

AND and OR logical operators in Scheme

I have a conditional, and I want to test to see if two things are true. How can I do an equivalent of && or || from Java in Scheme?

Upvotes: 1

Views: 19237

Answers (3)

erjiang
erjiang

Reputation: 45657

It's important to note that and and or don't return #t, but rather, the truthy value for which the condition was satisfied: either the last true value in and or the first true value in or.

(and 1 2) => 2
(and #f 2) => #f
(and #t 6) => 6
(or 1 2) => 1
(or #f #f 0 #f) => 0

Upvotes: 3

Will Hartung
Will Hartung

Reputation: 118651

Also note that not only does:

(and (equals? var1 var2) (equals? var3 var4))

work, but also:

(and (equals? var1 var2) (equals? var3 var4) (equals? var5 var6))

vs

(and (and (equals? var1 var2) (equals? var3 var4)) (equals? var5 var6))

(and ...) and (or ...) take any number of arguments.

Upvotes: 4

Corey
Corey

Reputation: 781

(and (equals? var1 var2) (equals? var3 var4))

Upvotes: 0

Related Questions