example.scm 1.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142
  1. (import
  2. (except (rnrs base)
  3. let-values
  4. map
  5. error)
  6. (only (guile)
  7. lambda* λ
  8. call-with-prompt
  9. abort-to-prompt
  10. make-prompt-tag))
  11. ;; Make a new prompt tag for use with call-with-prompt and
  12. ;; abort-to-prompt.
  13. (let ([prompt-tag (make-prompt-tag 'handled-sigint)])
  14. ;; Call a thunk within a new prompt, using the tag.
  15. (call-with-prompt prompt-tag
  16. ;; Thunk takes no arguments.
  17. (λ ()
  18. ;; Install a SIGINT handler (ctrl+c).
  19. (sigaction SIGINT
  20. (λ (signal)
  21. (abort-to-prompt prompt-tag 1 2 3)))
  22. ;; Run an endless loop, which the user will interrupt.
  23. (let loop ([i 0])
  24. (display (simple-format #f "at i=~a\n" i))
  25. (usleep 500000)
  26. (loop (+ i 1))))
  27. ;; Handler: This is the continuation of the program, which is used
  28. ;; when aborted to the prompt. The first argument is the "state of
  29. ;; the computation begun when thunk was called, and ending with
  30. ;; the call to abort-to-prompt". That state of the computation is
  31. ;; itself a continuation. The other arguments are whatever is
  32. ;; passed by the abort-to-prompt call.
  33. (λ (_state val1 val2 val3)
  34. ;; Display the value, with which the abort was done.
  35. (display
  36. (simple-format
  37. #f "aborted to prompt with values: ~a\n"
  38. val3)))))