开发者

Query on Scheme/Racket - 'if' clause

开发者 https://www.devze.com 2023-01-25 14:13 出处:网络
Currently learning Scheme/Racket and have problem running this piece of code. (if (or (< aftnHour 0) (> aftnHour 6))

Currently learning Scheme/Racket and have problem running this piece of code.

(if (or (< aftnHour 0) (> aftnHour 6))
  ((display 
      "You entered an invalid input. Please enter an input between 0 and 8 only.")
   (newline)(newline)(askAftnHour))

My goal is to check if a variable is not between 0 and 6. If that condition is satisfied, I want to prompt the user about his mistake and call the same method again. The newline is just for formatting purposes.

The code actually works the first time when the user enters wrongly, i.e. the error message is shown and then the function is called again. But now upon entering the correct input, an error is produced:开发者_如何转开发

"procedure application: expected procedure, given: #; arguments were: # # # "

I suspect I am doing something wrong with my newline, but really can't figure out what. Your help/advise is greatly appreciated.


You are missing a begin call before display. It's not enough to wrap some expressions in () and treat them as a sequence, they will be actually treated as a function call, in this example it's a void call with three void parameters.

> (if (or(< aftnHour 0)(> aftnHour 6))
>     (begin (display "You entered an invalid input. Please enter an input between 0
> and 8 only.
> ")(newline)(newline)(askAftnHour))
0

精彩评论

暂无评论...
验证码 换一张
取 消