[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]
Re: Matching lambdas proposal
From: |
Mark H Weaver |
Subject: |
Re: Matching lambdas proposal |
Date: |
Sat, 30 Nov 2013 14:09:46 -0500 |
User-agent: |
Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux) |
Hi,
Panicz Maciej Godek <address@hidden> 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