Having issues with cond

i am working through the htdp book online and i came across and excerise which asks me to

Then create an expression that converts the value of in to a non-negative number. For a String, it determines how long the String is; for an Image, it uses the area; for a Number, it uses the absolut value; for #true it uses 10 and for #false 20. Hint Check out cond from the Prologue: How to Program (again).

the last part is the issue it wants to write a predicate which returns 10 or 20 depending on the value of inn. this is my code so far

(define inn "abc")

  [(string? inn ) (string-length inn)]
  [(image? inn) ( + (image-width) (image-height))]
  [(number? inn) (abs inn)]
  [(boolean? inn #true)  (boolean? inn #false)] 10 20)

so if put false as the input of the def inn it should return 20.


I dot want to give you the answer and I have not tested(I may be wrong!) this but hopefully the following will help:

I think you need to split the last line into two separate cons clauses.

  • a cond clause is activated when the first part is true so you don't need boolean as inn already is a boolean.

  • how do you think you can get a #true when inn is #false? (hint: you might need a boolean operator)

You are very close - keep going :smiley:

best regards,

PS Check your area calculation

PPS: Use three backticks then the word scheme at the beginning and three backticks at the end to get nice formatting :smiley:

1 Like

Just a small note regarding formatting as code, scheme was set to be the default and thus can be omitted.

I fixed small syntactic mistakes in the last line:

(require 2htdp/image)

(define inn "abc")

  [(string? inn ) (string-length inn)]
  [(image? inn) ( + image-width image-height)]
  [(number? inn) (abs inn)]
  [(boolean? inn)  (if (boolean=? inn #false) 10 20)])

I restored another mistake so you aren’t “home” yet.

Recommendation: Open Help Desk. Search boolean?. Follow the link to the BSL version. Take a close look. ~~ While you’re there, explore some more of the functions that you might need | like | use in the future.