1
Fork 0
mirror of https://git.savannah.gnu.org/git/guile.git synced 2025-06-15 08:10:17 +02:00

add reset and shift

* module/ice-9/control.scm (reset, shift): Add implementations of these
  operators from Wolfgang J Moeller, derived from implementations by
  Oleg Kiselyov.
  (reset*, shift*): Procedural variants.

* test-suite/tests/control.test ("shift and reset"): Add tests,
  originally from Oleg Kiselyov.
This commit is contained in:
Andy Wingo 2011-04-28 12:17:56 +02:00
parent 6b480ced9c
commit 18e444b40e
2 changed files with 66 additions and 2 deletions

View file

@ -1,6 +1,6 @@
;;; Beyond call/cc
;; Copyright (C) 2010 Free Software Foundation, Inc.
;; Copyright (C) 2010, 2011 Free Software Foundation, Inc.
;;;; This library is free software; you can redistribute it and/or
;;;; modify it under the terms of the GNU Lesser General Public
@ -21,7 +21,7 @@
(define-module (ice-9 control)
#:re-export (call-with-prompt abort-to-prompt
default-prompt-tag make-prompt-tag)
#:export (% abort))
#:export (% abort shift reset shift* reset*))
(define (abort . args)
(apply abort-to-prompt (default-prompt-tag) args))
@ -54,3 +54,29 @@
(% (default-prompt-tag)
(proc k)
default-prompt-handler))
;; Kindly provided by Wolfgang J Moeller <wjm@heenes.com>, modelled
;; after the ones by Oleg Kiselyov in
;; http://okmij.org/ftp/Scheme/delim-control-n.scm, which are in the
;; public domain, as noted at the top of http://okmij.org/ftp/.
;;
(define-syntax reset
(syntax-rules ()
((_ . body)
(call-with-prompt (default-prompt-tag)
(lambda () . body)
(lambda (cont f) (f cont))))))
(define-syntax shift
(syntax-rules ()
((_ var . body)
(abort-to-prompt (default-prompt-tag)
(lambda (cont)
((lambda (var) (reset . body))
(lambda vals (reset (apply cont vals)))))))))
(define (reset* thunk)
(reset (thunk)))
(define (shift* fc)
(shift c (fc c)))