Try this instead:
(define (count2-iter1 num)
(define (loop idx)
(if (> idx num)
#t
(begin ; notice the difference!
(display idx)
(loop (+ idx 1)))))
(loop 1))
Here's why: when you use an if
, there can be only one expression in the consequent part and one in the alternative part. If more than one expression is needed, we have to surround them with a (begin ...)
. You surrounded the expressions between (...)
, which is not ok, because parenthesis are used for function application (that's why the error message states that The object #!unspecific is not applicable
).
On the other hand, a cond
has an implicit begin
for each of its clauses when a condition is met. Personally, I prefer to stick with using cond
when I need more than one expression after a condition - it's less verbose.