Page 1 of 1

Processing after a function returns

Posted: Mon Apr 26, 2010 3:46 pm
by bl108
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

Posted: Mon Apr 26, 2010 10:44 pm
by ramarren
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

Posted: Tue Apr 27, 2010 7:40 am
by nuntius
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

Posted: Tue Apr 27, 2010 12:11 pm
by bl108
That might work. May I ask how you would "print a value to the REPL" ?

Re: Processing after a function returns

Posted: Tue Apr 27, 2010 1:28 pm
by gugamilare
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 *:

Code: Select all

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

Posted: Tue Apr 27, 2010 2:14 pm
by bl108
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

Posted: Tue Apr 27, 2010 6:36 pm
by nuntius

Code: Select all

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