* Matching lambdas proposal
@ 2013-11-30 12:37 Panicz Maciej Godek
2013-11-30 19:09 ` Mark H Weaver
0 siblings, 1 reply; 3+ messages in thread
From: Panicz Maciej Godek @ 2013-11-30 12:37 UTC (permalink / raw)
To: guile-devel
[-- Attachment #1: Type: text/plain, Size: 1479 bytes --]
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?
[-- Attachment #2: Type: text/html, Size: 2073 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Matching lambdas proposal
2013-11-30 12:37 Matching lambdas proposal Panicz Maciej Godek
@ 2013-11-30 19:09 ` Mark H Weaver
2013-11-30 19:43 ` Panicz Maciej Godek
0 siblings, 1 reply; 3+ messages in thread
From: Mark H Weaver @ 2013-11-30 19:09 UTC (permalink / raw)
To: Panicz Maciej Godek; +Cc: guile-devel
Hi,
Panicz Maciej Godek <godek.maciek@gmail.com> writes:
> 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))
Although it's not in the manual, we already have this. It's called
'match-lambda' and 'match-lambda*' in (ice-9 match). 'match-lambda'
creates a procedure that accepts a single argument, and does pattern
matching on that argument. 'match-lambda*' matches the entire argument
list.
(use-modules (ice-9 match))
(define f (match-lambda ((a . b) (+ a b))))
(f (cons 3 4)) => 7
Mark
^ permalink raw reply [flat|nested] 3+ messages in thread
* Re: Matching lambdas proposal
2013-11-30 19:09 ` Mark H Weaver
@ 2013-11-30 19:43 ` Panicz Maciej Godek
0 siblings, 0 replies; 3+ messages in thread
From: Panicz Maciej Godek @ 2013-11-30 19:43 UTC (permalink / raw)
To: Mark H Weaver; +Cc: guile-devel
[-- Attachment #1: Type: text/plain, Size: 900 bytes --]
Hello :)
2013/11/30 Mark H Weaver <mhw@netris.org>
>
> Although it's not in the manual, we already have this. It's called
> 'match-lambda' and 'match-lambda*' in (ice-9 match). 'match-lambda'
> creates a procedure that accepts a single argument, and does pattern
> matching on that argument. 'match-lambda*' matches the entire argument
> list.
>
> (use-modules (ice-9 match))
> (define f (match-lambda ((a . b) (+ a b))))
> (f (cons 3 4)) => 7
>
>
My implementation actually does use the match-lambda* macro.
The whole idea was to extend the possibilities of the "regular"
lambda, just like the (ice-9 curried-definitions) extends the way
the define form can be used. For the time being, the notation
(lambda ((x y)) ...) is illegal, although I believe that it's quite
comprehensible and natural, and using it doesn't decrease
the understandability of the program, while making it more
concise.
[-- Attachment #2: Type: text/html, Size: 1584 bytes --]
^ permalink raw reply [flat|nested] 3+ messages in thread
end of thread, other threads:[~2013-11-30 19:43 UTC | newest]
Thread overview: 3+ messages (download: mbox.gz follow: Atom feed
-- links below jump to the message on this page --
2013-11-30 12:37 Matching lambdas proposal Panicz Maciej Godek
2013-11-30 19:09 ` Mark H Weaver
2013-11-30 19:43 ` Panicz Maciej Godek
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for read-only IMAP folder(s) and NNTP newsgroup(s).