3

Is there a shorthand in scheme for ((lambda () ))

For example, instead of

((lambda ()
    (define x 1)
    (display x)))

I would love to be able to do something like

(empty-lambda
    (define x 1)
    (display x))
Cam
  • 14,930
  • 16
  • 77
  • 128

4 Answers4

10

The usual idiom for that is

(let ()
  (define x 1)
  (display x))

which you can of course turn into a quick macro:

(define-syntax-rule (block E ...) (let () E ...))
Eli Barzilay
  • 29,301
  • 3
  • 67
  • 110
  • 2
    I thought the obvious answer would be to use `begin`, like: `(begin (define x 1) (display x))` Turns out to be obvious and wrong, see http://stackoverflow.com/questions/1683796/how-can-you-rewrite-begin-in-scheme/1683842#1683842. – Shannon Severance Oct 18 '11 at 21:11
3

Why not just

(let
    ((x 1))
    (display x))
newacct
  • 119,665
  • 29
  • 163
  • 224
3

Racket provides the block form, which works like this:

#lang racket
(require racket/block)
(block
 (define x 1)
 (display x))
Sam Tobin-Hochstadt
  • 4,983
  • 1
  • 21
  • 43
2
(define-syntax empty-lambda
  (syntax-rules ()
    ((empty-lambda body ...)
      ((lambda () body ...)))))
Bill the Lizard
  • 398,270
  • 210
  • 566
  • 880
user448810
  • 17,381
  • 4
  • 34
  • 59