guile-devel
[Top][All Lists]
Advanced

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Matching lambdas proposal


From: Panicz Maciej Godek
Subject: Matching lambdas proposal
Date: Sat, 30 Nov 2013 13:37:37 +0100

Hi,
I recently came up with the following idea:
similarly to curried definitions (from (ice-9 curried-definitions)),
one could introduce matching lambdas, i.e.
lambdas whose arguments can be destructured
in the argument list, so that instead of e.g.

(define (f pair)
  (+ (car pair) (cdr pair)))

one could write 

(define (f (a . b))
  (+ a b))

The extension is compatible with curried-definitions
and could be performed alongside (requires srfi-1 and
ice-9 match modules, though):

(define-syntax mlambda
  (lambda (stx)
    (syntax-case stx ()
      ((_ (first-arg ... last-arg . rest-args) body body* ...)
       (and (every identifier? #'(first-arg ... last-arg))
            (or (identifier? #'rest-args) (null? #'rest-args)))
       #'(lambda (first-arg ... last-arg . rest-args) body body* ...))
      ((_ arg body body* ...)
       (or (identifier? #'arg) (null? #'arg))
       #'(lambda arg body body* ...))
      ((_ args body body* ...)
       #'(match-lambda* (args body body* ...))))))

(define-syntax cdefine
  (syntax-rules ()
    ((_ ((head . tail) . args) body body* ...)
     (cdefine (head . tail)
       (mlambda args body body* ...)))
    ((_ (name . args) body body* ...)
     (define name (mlambda args body body* ...)))
    ((_ . rest)
     (define . rest))))

(obviously, mlambda should replace lambda
just as cdefine replaces define)

I think that it is would be a nice extension,
because it only increases the expressive power
of the language.
WDYT?

reply via email to

[Prev in Thread] Current Thread [Next in Thread]