[plt-scheme] Binding display function with outputting to textfile.

From: Jon Rafkind (rafkind at cs.utah.edu)
Date: Wed Dec 10 13:52:12 EST 2008

>  
> (define o (open-output-file "helloworld.txt"))
>  
> (define hello
>   (display "hello world" o) )
>  
> (close-output-port o)
>  
>  
> Why does "hello world" get written to helloworld.txt when I run this, 
> even though it is within a function, and I never called that function 
> in my interaction window?

Because those definitions aren't functions. There is some trickery that 
`define' uses to create a function for you.
(define x 1) ;; x is now 1 when you click run
(define (x) 1) ;; x is a function that returns 1

(define (x) 1) is shorthand for (define x (lambda () 1). So you have 
(define o (open-output-file "hello.txt")), and as soon as you click 
'run' the (open-output-file ...) will be executed. Same for `hello'. All 
the code will be executed as soon as you click run.



Posted on the users mailing list.