Search code examples
recursionschememit-scheme

Display to output port in a recursive procedure - Scheme


I am learning Scheme and want to write a recursive procedure which output to the console in each run level:

(define (dummy count)
    (if (= 0 count)        
        (runtime)
        ((display "test" console-i/o-port) (dummy (- count 1)))))

And then test with:

(dummy 10)

But it appears that only the output of the last procedure called will be printed out. What should I do to make it happen? Thanks. (I am using Mit-scheme)


Solution

  • ((display "test" console-i/o-port) (dummy (- count 1)))
    

    This is a function call where (display "test" console-i/o-port) is the function that's supposed to be called and (dummy (- count 1)) is the argument to that function. Since `(display "test" console-i/o-port) does not actually return a function, this will cause an error (after printing test).

    To do what you actually want to do (first execute (display ...) and then execute (dummy ...)), you can use the begin form like this:

    (begin (display "test" console-i/o-port) (dummy (- count 1)))