David542
David542

Reputation: 110277

Proper way to pass a boolean function

I thought to see if something was "true" in scheme I could do the following:

(lambda (x) (= x #t)))

However, that gives me some sort of error, and I fell back to use something like the following which works (for now):

(lambda (x) x)

What would be the closest to a isTrue function in scheme similar to the python:

>>> bool(1)
True
>>> bool(0)
False

Also, why doesn't doing something like (= 4 #t) work? Does = only work on numeric types in scheme?

Upvotes: 2

Views: 394

Answers (1)

Óscar López
Óscar López

Reputation: 236034

For testing if something is false in Racket, we have the false? predicate, but curiously we don't have a true? predicate - it's easy enough to implement, though:

(define (true? exp)
  (not (false? exp)))

In case you're wondering, in Scheme the only false value is #f, everything else is considered truthy. And you're right, the = procedure is used exclusively for numbers; if you need a more general equality comparison simply use equals?. This works now:

(equal? 4 #t)
=> #f

Clearly 4 is not equal to #t, but anyway 4 (or any other number for that matter) is considered truthy:

(if 4 'ok 'nope)
=> 'ok

Upvotes: 4

Related Questions