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.
signature.asc
Description: PGP signature

