简体   繁体   中英

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)

((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)))

If what you want to do is displaying "test" count number of times (10 in the example) you can do something like this (assuming that count is positive):

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM