Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to implement a time limited execution mechanism in CLISP?

What I have in mind is something like:

(run (long-calculation vars) time-limit)

which returns the result of (long-calculation vars) or nil if time-limit is reached.

like image 341
matejch Avatar asked Oct 24 '22 00:10

matejch


2 Answers

If you could find an implementation for the amb operator, then you could do something like this:

(defmacro run (comp time-limit)
  `(amb comp
       (progn (delay ,time-limit)
              nil)))

Be careful not to mistake this with the McCarthy amb operator. amb should evaluate both its arguments in separate threads and pick whichever finishes first. For example, in Haskell, it's described in Data.Unamb.

like image 99
Peteris Avatar answered Nov 01 '22 10:11

Peteris


I would suggest using bordeaux-threads, spawning a thread for the computation, and if it doesn't return by the timer, reaping the thread and returning nil.

like image 23
Paul Nathan Avatar answered Nov 01 '22 12:11

Paul Nathan