読者です 読者をやめる 読者になる 読者になる

Dr.Schemeのlocal

これで良いかな…

(define-macro (local defs . body)
  (let* ((split-define
          (lambda (def)
            (let ((name (cadr def))
                  (body (cddr def)))
              (cond [(list? name)
                     (cons (car name) (list (cons 'lambda (cons (cdr name) body))))]
                    [(pair? name)
                     (cons (car name) (list (cons 'lambda (cons (cdr name) body))))]
                    [else (cons name body)]))))
         (def-pairs (map split-define defs))
         (init (map (lambda (p) (list (car p) #f)) def-pairs))
         (setup (map (lambda (p) (cons 'set! (cons (car p) (cdr p)))) def-pairs)))
    (cons 'let (cons init (append setup body)))))

(local ((define x 1)) x)
(local ((define (f x) (+ x 1))) (f 2))
(local ((define (f . x) (print x))) (f 1 2 3))