This is a read-only archive of lispforum.com. The forum was locked to new users and posts and is preserved here as static HTML from a database snapshot taken on 2019-09-07.

Processing after a function returns

7 posts · 2013 views

Hello,

Is there a way in Common Lisp to continue processing data, after the REPL has returned a value - without starting a subprocess.

(defun foo (x y)
(prog1 (+ x y)
(do more processing after (+ x y) has been returned...))

Thank you.

Re: Processing after a function returns

You can create a thread on implementations supporting threads. For example, SBCL manual on threading. I suppose it would be possible to somehow use multiplexing to interleave some computation directly with the REPL, if you implemented your own REPL (I think Slime has multiplexing mode on some implementations), but what would be the point?

Re: Processing after a function returns

Could your program simply print a value to the REPL and continue processing? The return value can't be used until the current function completes...

Re: Processing after a function returns

That might work. May I ask how you would "print a value to the REPL" ?

Re: Processing after a function returns

bl108 wrote:That might work. May I ask how you would "print a value to the REPL" ?
If you evaluate some form in the REPL like (+ 1 2), the result is already printed to the REPL and stored in the variable *:
cl-user> (+ 1 2)
3
cl-user> *
3
cl-user> (+ * 5)
8
You can forcefully print a value using print or format (among others like prin1, princ, which vary the way the value is printed).

Re: Processing after a function returns

How would you do that the processing occured after foo has returned it's value ? If the print statement were in the function, then that would run before foo ended.

(defun foo (x y)
(prog1 (+ x y)
(do more processing after (+ x y) has been returned...))

Re: Processing after a function returns

(defun double-square (x)
  (print (+ x x))
  (* x x))
Once a function returns, it cannot do anything else. It spawns another thread or process for the OS to schedule, or register an event for an in-process event loop to pick up. With a little work, the function could also return a new function (e.g. a continuation) for continued processing.

Until a function returns, nobody else can access its return value. The only ways to pass values without returning are to call another function, to write to memory for another thread, or use IPC to communicate with another process.