Processing after a function returns

Discussion of Common Lisp
Post Reply
bl108
Posts: 3
Joined: Mon Apr 26, 2010 3:30 pm

Processing after a function returns

Post by bl108 » Mon Apr 26, 2010 3:46 pm

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.

ramarren
Posts: 613
Joined: Sun Jun 29, 2008 4:02 am
Location: Warsaw, Poland
Contact:

Re: Processing after a function returns

Post by ramarren » Mon Apr 26, 2010 10:44 pm

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?

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Processing after a function returns

Post by nuntius » Tue Apr 27, 2010 7:40 am

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

bl108
Posts: 3
Joined: Mon Apr 26, 2010 3:30 pm

Re: Processing after a function returns

Post by bl108 » Tue Apr 27, 2010 12:11 pm

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

gugamilare
Posts: 406
Joined: Sat Mar 07, 2009 6:17 pm
Location: Brazil
Contact:

Re: Processing after a function returns

Post by gugamilare » Tue Apr 27, 2010 1:28 pm

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

bl108
Posts: 3
Joined: Mon Apr 26, 2010 3:30 pm

Re: Processing after a function returns

Post by bl108 » Tue Apr 27, 2010 2:14 pm

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

nuntius
Posts: 538
Joined: Sat Aug 09, 2008 10:44 am
Location: Newton, MA

Re: Processing after a function returns

Post by nuntius » Tue Apr 27, 2010 6:36 pm

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.

Post Reply