|
|
|
|
|
by such_a_casual
3789 days ago
|
|
I haven't programmed in C, but my view comes from this video:
https://www.youtube.com/watch?v=QM1iUe6IofM&feature=youtu.be... What would be the C equivalent of: (let ((last-response))
(defun get-response ()
(print last-response)
(setf last-response (read))))
(print (get-response))
(print (get-response))
Notice that no other function can use last-response. It only exists for get-response, but it exists outside the scope of get-response. Does C have this? |
|
Since the variable in question is only used in one function, you could declare the variable static to that function. It takes some care to make this reentrant, but that's possibly the case in lisp as well.
To share between multiple top-level functions, you can only limit scope to the individual file.
The bigger thing C can't do that lisp can here is defining new functions in arbitrary places. In C variants where you can define local functions, you can indeed capture variables in local scope. And within a function, you can limit scope by creating a new block:
Even in those C variants, you can't place a new function in global scope from within a function.