The others have answered your explicit question, but one minor detail:
 Racket regards #f as the only false value.  Everything else is true,
which means that if there's a difference between these two tests:

(if foo                   "true" "false")
(if (equal? #t foo) "true" "false")

This can be relevant if you have functions with contracts that specify
they will return a boolean.  For example:

(define (do-tests x)
   (unless (> x 7) (raise "x should be > 7"))
   (unless (< x 20) (raise "x should be less than 20"))
   (add1 x))

(displayln (if (do-tests 9) "Success!"  "Failure!")))

This prints "Success!"

Add a contract:

(define/contract (do-tests x)
   (-> integer? boolean?)
   (unless (> x 7) (raise "x should be > 7"))
   (unless (< x 20) (raise "x should be less than 20"))
   x)

(displayln (if (do-tests 9) "Success!"  "Failure!")))

...and it throws an exception saying "do-tests broke its own
contract", because it returned an integer instead of either #t or #f.

This is very rarely an issue, but if you absolutely positively must
have a boolean value then you can do this:  (not (not x)) which will
force x to the boolean representation of its truth value -- that is,
the value #f will continue to be #f and any other value will be
converted to #t.



On Sat, Apr 15, 2017 at 11:51 AM, 'William J. Bowman' via Racket Users
<[email protected]> wrote:
> On Sat, Apr 15, 2017 at 07:16:49AM -0700, Angus wrote:
>> I have a function that can return either false or a node.  I have to check 
>> four permutations:
>>
>> both nodes false,
>> left node false,
>> right node valid, left node false
>> both node valid
>>
>> I am doing it using cond like this:
>>
>> (define (lines t)
>>   (cond [(and (false? (node-l t)) (false? (node-r t))) <<do no children 
>> thing>>]  ;; if no children
>>         [(and (false? (node-l t)) (not (node-r t)))  <<left child only>>]  
>> ;; if left child only
>>         [(and (node-l t) (false? (node-r t))) <<right child only>> ]  ;; if 
>> right child only
>>         [else <<both children>> ]))  ;; if both children
> This looks like it might be a homework assignment. What Matthew said is true, 
> you can make this
> shorter, but if your class encourages a specific explicit style then you 
> should be explicit instead.
>
>> the first cond line using and and false? works ok and the last cond line 
>> using else works ok.  But how do I check for the middle two occurrences?
> Any value that is not false in Racket is true. You can test for non-false 
> values using any conditional
> construct, like `if` or `cond`. The operator `not` changes any non-false 
> value into false, and
> false into true.
>
>
> #lang racket
> (define x 1)
> (define y "Hello, World")
> (define z false)
>
> (displayln x)
> (displayln (not x))
> (displayln (not z))
> (displayln (not (not z)))
>
> (if x
>     (displayln x)
>     (displayln "This can't happen"))
>
> (if y
>     (displayln y)
>     (displayln "This also can't happen"))
>
> (cond
>    [(and x y (not z))
>     (printf "~a and ~a and not ~a~n" x y z)]
>    [else (displayln "This cannot happen either")])
>
>
> --
> William J. Bowman
> Northeastern University
> College of Computer and Information Science
>
> --
> You received this message because you are subscribed to the Google Groups 
> "Racket Users" group.
> To unsubscribe from this group and stop receiving emails from it, send an 
> email to [email protected].
> For more options, visit https://groups.google.com/d/optout.

-- 
You received this message because you are subscribed to the Google Groups 
"Racket Users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to