srfi-11.stk 2.98 KB
Newer Older
eg's avatar
eg committed
1 2
;;;;
;;;; srfi-11.stk	-- LET-VALUES and LET*-VALUES
3 4 5 6
;;;;
;;;; Copyright © 2002-2007 Erick Gallesio - I3S-CNRS/ESSI <eg@unice.fr>
;;;;
;;;;
eg's avatar
eg committed
7 8 9 10
;;;; This program is free software; you can redistribute it and/or modify
;;;; it under the terms of the GNU General Public License as published by
;;;; the Free Software Foundation; either version 2 of the License, or
;;;; (at your option) any later version.
11
;;;;
eg's avatar
eg committed
12 13 14 15
;;;; This program is distributed in the hope that it will be useful,
;;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;;;; GNU General Public License for more details.
16
;;;;
eg's avatar
eg committed
17 18
;;;; You should have received a copy of the GNU General Public License
;;;; along with this program; if not, write to the Free Software
19
;;;; Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA 02111-1307,
eg's avatar
eg committed
20
;;;; USA.
21
;;;;
eg's avatar
eg committed
22 23
;;;;           Author: Erick Gallesio [eg@essi.fr]
;;;;    Creation date: 28-May-2002 09:47 (eg)
Erick Gallesio's avatar
Erick Gallesio committed
24
;;;; Last file update:  4-May-2007 19:41 (eg)
eg's avatar
eg committed
25 26 27 28 29 30 31
;;;;


(define-syntax let-values
  (syntax-rules ()
    ((let-values (?binding ...) ?body0 ?body1 ...)
     (let-values "bind" (?binding ...) () (begin ?body0 ?body1 ...)))
32

eg's avatar
eg committed
33 34
    ((let-values "bind" () ?tmps ?body)
     (let ?tmps ?body))
35

eg's avatar
eg committed
36 37
    ((let-values "bind" ((?b0 ?e0) ?binding ...) ?tmps ?body)
     (let-values "mktmp" ?b0 ?e0 () (?binding ...) ?tmps ?body))
38

eg's avatar
eg committed
39
    ((let-values "mktmp" () ?e0 ?args ?bindings ?tmps ?body)
40
     (call-with-values
eg's avatar
eg committed
41 42 43
       (lambda () ?e0)
       (lambda ?args
         (let-values "bind" ?bindings ?tmps ?body))))
44

eg's avatar
eg committed
45 46
    ((let-values "mktmp" (?a . ?b) ?e0 (?arg ...) ?bindings (?tmp ...) ?body)
     (let-values "mktmp" ?b ?e0 (?arg ... %%x) ?bindings (?tmp ... (?a %%x)) ?body))
47

eg's avatar
eg committed
48 49 50 51 52 53 54 55 56 57 58 59 60 61 62
    ((let-values "mktmp" ?a ?e0 (?arg ...) ?bindings (?tmp ...) ?body)
     (call-with-values
       (lambda () ?e0)
       (lambda (?arg ... . %%x)
         (let-values "bind" ?bindings (?tmp ... (?a %%x)) ?body))))))

(define-syntax let*-values
  (syntax-rules ()
    ((let*-values () ?body0 ?body1 ...)
     (begin ?body0 ?body1 ...))

    ((let*-values (?binding0 ?binding1 ...) ?body0 ?body1 ...)
     (let-values (?binding0)
       (let*-values (?binding1 ...) ?body0 ?body1 ...)))))

Erick Gallesio's avatar
Erick Gallesio committed
63 64

(export-syntax let-values let*-values)
eg's avatar
eg committed
65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95
(provide "srfi-11")

#|
Usage examples:

(let-values (((a b . c) (values 1 2 3 4)))
	    (list a b c))

(let ((a 'a) (b 'b) (x 'x) (y 'y))
  (let-values (((a b) (values x y))
	       ((x y) (values a b)))
	      (list a b x y)))

(let ((a 'a) (b 'b) (x 'x) (y 'y))
  (let*-values (((a b) (values x y))
		((x y) (values a b)))
	       (list a b x y)))
|#

;//
;//(call-with-values
;//  (lambda () (values 1 2 3 4))
;//  (lambda (a b . c)
;//     (list a b c)))
;//
;//(define-macro (let-values bindings . body)
;//  (if (and (pair? bindings) (null? (cdr bindings)))
;//      `(call-with-values
;//	   (lambda () ,(cadar bindings))
;//	   (lambda ,(caar bindings)
;//	     ,@body))))
96
;//
eg's avatar
eg committed
97 98 99 100
;//
;//(macro-expand '(let-values (((a b . c) (values 1 2 3 4)))
;//	    (list a b c)))
;//