Search code examples
comparisonschemebooleancomputer-scienceboolean-expression

Boolean Comparison


I'm doing a few exercises from HtDP (How to Design Programs) and I am kind of stuck on the Boolean comparison question. It goes like this.

    (define b1 true)

    (define b2 false)

Write an expression that computes whether b1 is false and b2 is true. If false produce No and vice versa.

Right now this is all I have come up with:

   (and b1 true) => true 

(Shameless. I know but I am new at this and I am really slow to catch on)

Any help you could give me would be match appreciated.

Thanks


Solution

  • It's pretty straightforward to translate the question into code. As a first approach, let's copy the question verbatim in pseudocode:

    (b1 == false) AND (b2 == true)
    

    Now, how would you write the above in Scheme? remember, Scheme uses prefix notation

    (<???> (<???> b1 false) (<???> b2 true))
    

    With a bit more of practice, the same code can be written more compactly like this (again, first in pseudocode):

    NOT b1 AND b2
    

    Which should be simple enough to write in Scheme:

    (<???> (<???> b1) b2)