Why does my code not work? How can I fix it?

(define (brew-time grams)
  (define water (* 100 (/ grams 6)))
   (cond
     [(< water 100) (error 'brew-time "Zu geringe Menge")]
     [(> water 800) (error 'brew-time "Zu grosse Menge")]
     [(= water 100) 60]
     [(and (>= water 100) ( <= water 300) (* (ceiling (/ water 100)) 60))]
     [(and (> water 300) (<= water 800) (+ 90 (* (ceiling (/ water 100) 30))))]))

I am getting the syntax error:

BrewTime.rkt:57:3: define: expected only one expression for the function body, but found 1 extra part

If you're using the Student Languages, a define can only have one expression in it: that is, it has to look like

(define (function arguments...)
    (... ...))

but your define has two expressions, the (define water...) definition and the cond.

If you're using BSL, rewrite it so that water is a helper function that takes a weight of water as its input.
If you're using ISL or ASL, wrap your definitions in local:

(define (brew-time grams)
  (local
    [(define water ...)]
    (cond
      [...]
      [...])))
1 Like

Thank you very much, that already helps a lot. I am using BSL, But how exactly can I rewrite water to a helper function

It'll look like

(define (water grams) (...))
(define (brew-time grams) (cond ...))

and brew-time will use water within it.

Hey, thank you. I tried this:

(define (water grams)
  (* 100 (/ grams 6)))

(define (brew-time grams)
   (cond
     [(< water 100) (error 'brew-time "Zu geringe Menge")]
     [(> water 800) (error 'brew-time "Zu grosse Menge")]
     [(= water 100) 60]
     [(and (>= water 100) ( <= water 300) (* (ceiling (/ water 100)) 60))]
     [(and (> water 300) (<= water 800) (+ 90 (* (ceiling (/ water 100) 30))))]))

but now I have the error:
water: expected a function call, but there is no open parenthesis before this function in: water

That's half the fix, the other half is to go into brew-time and replace water with function calls now that it's a function and not a value.

Since it looks like grams is not used after water is computed, it might be simpler to refactor this as

(define (brew-time/helper water) ...)
(define (brew-time grams)
  (brew-time/helper (* 100 (/ grams 6))))

... and then just use your existing code in brew-time/helper.

This does seem like an ideal case for a local definition, though, I agree that your original code was quite readable and nice.

(Also, maybe you want to bump it up to 75g/l ? No? Ah well.)

1 Like