bsilverstrim
2022-5-31 15:23:35

Can someone clarify what’s happening? I’m working through the tutorial for more systems programming in Racket (https://docs.racket-lang.org/more/) and it recommended using the Racket CLI to run the exercises. At a point where it is fixing the “port reuse” problem, the documents include a way to have the application stop (because it seems control-C will give you control back in the REPL, but the program continues running…the listener was still on the port so it can’t be re-run without changing ports). The instructions use "(define stop (serve 8081))" after some changes to the source, but this seems to immediately run the simple web server. I thought that the “define” would just…define a function? Then I actually execute "(stop)" to stop the program from running. How is the “define” actually executing the program instead of just defining the “stop” function, and running “stop”, which should be a function that runs “serve 8081”, stops the application?


samth
2022-5-31 15:27:37

(define (f x) ...) defines a function and doesn’t run any code. (define f ...) defines f to be the result of running the code on the right hand side, which in your case is (serve 8081).


bsilverstrim
2022-5-31 15:30:36

Oh, so the (define stop (serve 8081)) is actually saying that stop is going to be what serve 8081 returns, meaning it has to run, so I’m telling it to go execute serve and when I do (stop) it’s getting the “final evaluation” of the result and that ends up closing the application?


samth
2022-5-31 15:32:41

Not quite. stop is indeed defined to be what (serve 8081) returns. That thing is a function (the lambda on line 12) which stops the server.


bsilverstrim
2022-5-31 15:33:19

Thanks!