guile-devel
[Top][All Lists]
Advanced

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

Re: Shorter lambda expressions


From: Mark H Weaver
Subject: Re: Shorter lambda expressions
Date: Fri, 24 Jan 2014 11:09:39 -0500
User-agent: Gnus/5.13 (Gnus v5.13) Emacs/24.3 (gnu/linux)

address@hidden (Taylan Ulrich "Bayırlı/Kammer") writes:

> address@hidden (Ludovic Courtès) writes:
>
>> One related thing I sometimes lack is:
>>
>>   (define-syntax-rule (thunk exp ...)
>>     (lambda () exp ...))
>
> My understanding is that that'd be (^ exp ...).

That's true given the code I posted, but it was not intentional.  I
meant to follow Gauche's convention that '^' is an alias for 'lambda',
so you'd need to type (^() exp ...) instead.

Another bug is that I should have used (expand load eval) in the
'eval-when'.

Finally, I've decided that it's nicer to export 'define-short-lambda'
than 'short-lambda'.

Here's an updated version.  More thoughts?  Alas, I guess this entire
topic is bikeshed territory :)

     Mark


(define-module (ice-9 short-lambdas)
  #:export (^ ^_ λ_
              ^a ^b ^c ^d ^e ^f ^g ^h ^i ^j ^k ^l ^m
              ^n ^o ^p ^q ^r ^s ^t ^u ^v ^w ^x ^y ^z
              ^xy ^xyz ^ab ^abc ^uv
              λa λb λc λd λe λf λg λh λi λj λk λl λm
              λn λo λp λq λr λs λt λu λv λw λx λy λz
              λxy λxyz λab λabc λuv
              define-short-lambda))

(eval-when (expand load eval)
  (define short-lambda
    (lambda (form)
      (syntax-case form ()
        ((k-id body0 body ...)
         (let* ((k-symbol (syntax->datum #'k-id))
                (k-name   (symbol->string k-symbol))
                (chars    (cdr (string->list k-name)))
                (names    (map string chars))
                (symbols  (map string->symbol names)))
           (define (sym->id sym)
             (case sym
               ((_) (car (generate-temporaries '(_))))
               (else (datum->syntax #'k-id sym))))
           (with-syntax ((ids (map sym->id symbols)))
             #'(lambda ids body0 body ...))))))))

(define-syntax-rule (define-short-lambda k)
  (define-syntax k short-lambda))

(define-syntax-rule (define-short-lambdas k ...)
  (begin (define-short-lambda k) ...))

(define-syntax-rule (^ formals body0 body ...)
  (lambda formals body0 body ...))

(define-short-lambdas
  ^_ λ_
  ^a ^b ^c ^d ^e ^f ^g ^h ^i ^j ^k ^l ^m
  ^n ^o ^p ^q ^r ^s ^t ^u ^v ^w ^x ^y ^z
  ^xy ^xyz ^ab ^abc ^uv
  λa λb λc λd λe λf λg λh λi λj λk λl λm
  λn λo λp λq λr λs λt λu λv λw λx λy λz
  λxy λxyz λab λabc λuv)

reply via email to

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