* convert.c: include <string.h> for convert_i.c.
[bpt/guile.git] / srfi / srfi-16.scm
CommitLineData
6851c8a4
MG
1;;;; srfi-16.scm --- `case-lambda' for Guile
2
3;;; Copyright (C) 2001 Free Software Foundation, Inc.
4;;;
5;;; This program is free software; you can redistribute it and/or
6;;; modify it under the terms of the GNU General Public License as
7;;; published by the Free Software Foundation; either version 2, or
8;;; (at your option) any later version.
9;;;
10;;; This program is distributed in the hope that it will be useful,
11;;; but WITHOUT ANY WARRANTY; without even the implied warranty of
12;;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
13;;; General Public License for more details.
14;;;
15;;; You should have received a copy of the GNU General Public License
16;;; along with this software; see the file COPYING. If not, write to
17;;; the Free Software Foundation, Inc., 59 Temple Place, Suite 330,
18;;; Boston, MA 02111-1307 USA
f480396b
MV
19;;;
20;;; As a special exception, the Free Software Foundation gives permission
21;;; for additional uses of the text contained in its release of GUILE.
22;;;
23;;; The exception is that, if you link the GUILE library with other files
24;;; to produce an executable, this does not by itself cause the
25;;; resulting executable to be covered by the GNU General Public License.
26;;; Your use of that executable is in no way restricted on account of
27;;; linking the GUILE library code into it.
28;;;
29;;; This exception does not however invalidate any other reasons why
30;;; the executable file might be covered by the GNU General Public License.
31;;;
32;;; This exception applies only to the code released by the
33;;; Free Software Foundation under the name GUILE. If you copy
34;;; code from other Free Software Foundation releases into a copy of
35;;; GUILE, as the General Public License permits, the exception does
36;;; not apply to the code that you add in this way. To avoid misleading
37;;; anyone as to the status of such modified files, you must delete
38;;; this exception notice from them.
39;;;
40;;; If you write modifications of your own for GUILE, it is your choice
41;;; whether to permit this exception to apply to your modifications.
42;;; If you do not wish that, delete this exception notice.
6851c8a4
MG
43
44;;; Commentary:
45
46;;; Implementation of SRFI-16. `case-lambda' is a syntactic form
47;;; which permits writing functions acting different according to the
48;;; number of arguments passed.
49;;;
50;;; The syntax of the `case-lambda' form is defined in the following
51;;; EBNF grammar.
52;;;
53;;; <case-lambda>
54;;; --> (case-lambda <case-lambda-clause>)
55;;; <case-lambda-clause>
56;;; --> (<signature> <definition-or-command>*)
57;;; <signature>
58;;; --> (<identifier>*)
59;;; | (<identifier>* . <identifier>)
60;;; | <identifier>
61;;;
62;;; The value returned by a `case-lambda' form is a procedure which
63;;; matches the number of actual arguments against the signatures in
64;;; the various clauses, in order. The first matching clause is
65;;; selected, the corresponding values from the actual parameter list
66;;; are bound to the variable names in the clauses and the body of the
67;;; clause is evaluated.
68
69;;; Author: Martin Grabmueller
70
71;;; Code:
1a179b03
MD
72(define-module (srfi srfi-16)
73 :export-syntax (case-lambda))
6851c8a4 74
1b2f40b9
MG
75(cond-expand-provide (current-module) '(srfi-16))
76
6851c8a4
MG
77(define-macro (case-lambda . clauses)
78
79 ;; Return the length of the list @var{l}, but allow dotted list.
80 ;;
81 (define (alength l)
82 (cond ((null? l) 0)
83 ((pair? l) (+ 1 (alength (cdr l))))
84 (else 0)))
85
86 ;; Return @code{#t} if @var{l} is a dotted list, @code{#f} if it is
87 ;; a normal list.
88 ;;
89 (define (dotted? l)
90 (cond ((null? l) #f)
91 ((pair? l) (dotted? (cdr l)))
92 (else #t)))
93
94 ;; Return the expression for accessing the @var{index}th element of
95 ;; the list called @var{args-name}. If @var{tail?} is true, code
96 ;; for accessing the list-tail is generated, otherwise for accessing
97 ;; the list element itself.
98 ;;
99 (define (accessor args-name index tail?)
100 (if tail?
101 (case index
102 ((0) `,args-name)
103 ((1) `(cdr ,args-name))
104 ((2) `(cddr ,args-name))
105 ((3) `(cdddr ,args-name))
106 ((4) `(cddddr ,args-name))
107 (else `(list-tail ,args-name ,index)))
108 (case index
109 ((0) `(car ,args-name))
110 ((1) `(cadr ,args-name))
111 ((2) `(caddr ,args-name))
112 ((3) `(cadddr ,args-name))
113 (else `(list-ref ,args-name ,index)))))
114
115 ;; Generate the binding lists of the variables of one case-lambda
116 ;; clause. @var{vars} is the (possibly dotted) list of variables
117 ;; and @var{args-name} is the generated name used for the argument
118 ;; list.
119 ;;
120 (define (gen-temps vars args-name)
121 (let lp ((v vars) (i 0))
122 (cond ((null? v) '())
123 ((pair? v)
124 (cons `(,(car v) ,(accessor args-name i #f))
125 (lp (cdr v) (+ i 1))))
126 (else `((,v ,(accessor args-name i #t)))))))
127
128 ;; Generate the cond clauses for each of the clauses of case-lambda,
129 ;; including the parameter count check, binding of the parameters
130 ;; and the code of the corresponding body.
131 ;;
132 (define (gen-clauses l length-name args-name)
133 (cond ((null? l) (list '(else (error "too few arguments"))))
134 (else
135 (cons
136 `((,(if (dotted? (caar l)) '>= '=)
137 ,length-name ,(alength (caar l)))
138 (let ,(gen-temps (caar l) args-name)
139 ,@(cdar l)))
140 (gen-clauses (cdr l) length-name args-name)))))
141
142 (let ((args-name (gensym))
143 (length-name (gensym)))
144 (let ((proc
145 `(lambda ,args-name
146 (let ((,length-name (length ,args-name)))
147 (cond ,@(gen-clauses clauses length-name args-name))))))
148 proc)))