durandaltheta
durandaltheta

Reputation: 255

How can I construct procedures for usage in racket lisp engines?

I would like to use racket lisp engines which allow execution of a procedure that can be interrupted by a timer timeout. I'm not sure how to construct a procedure that engine will accept because I can't find examples online. At the engine documentation it lists input proc having the following contract:

(engine proc) → engine?
  proc : ((any/c . -> . void?) . -> . any/c)

I'm just learning typed racket annotation semantics and this is above my head at the moment. Can someone help provide some concrete examples of valid procedures that can be use in a racket engine?

Upvotes: 2

Views: 88

Answers (1)

Sylwester
Sylwester

Reputation: 48745

I've played a little around with it. Here is what I did:

#lang racket
(require racket/engine)

(define (test-engine allow-interrupt)
  (let loop ((n 1))
    (allow-interrupt #f)
    (displayln (list 'step n))
    (allow-interrupt #t)
    (sleep 1)
    (loop (add1 n))))

(define tee (engine test-engine))
(engine-run 2000 tee)

I noticed that it might break in the middle of a displayln so to make displayln atomic I made use of the supplied procedure that makes delays the interruption during atomic operations. Without it it will print out the rest in the next (engine-run 2000 tee) instead of finishing it before it returns.

Upvotes: 3

Related Questions