Thank you for your comment

Beau­tiful Racket / explainers

  1. (define x 42)
    x ; 42

    (define y 100)
    (let ([y 42])
      (+ x y)) ; 84 (not 142)

    (define z 1000)
    (define f (lambda ([z 42]) (+ x y z)))
    (f) ; 184 (not 1142)
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    (define x 42)
    x ; 42
    
    (define y 100)
    (let ([y 42])
      (+ x y)) ; 84 (not 142)
    
    (define z 1000)
    (define f (lambda ([z 42]) (+ x y z)))
    (f) ; 184 (not 1142)
    
    copy to clipboard
  2. (module mod1 br
      (define foo "zim")
      (provide foo))
    (require (submod "." mod1))

    foo ; "zim"

    (module mod2 br
      (define bar "zam")
      (provide bar))
    (require (submod "." mod2))

    (define bar "bang")
    bar ; "bang" (not "zam")
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    (module mod1 br
      (define foo "zim")
      (provide foo))
    (require (submod "." mod1))
    
    foo ; "zim"
    
    (module mod2 br
      (define bar "zam")
      (provide bar))
    (require (submod "." mod2))
    
    (define bar "bang")
    bar ; "bang" (not "zam")
    
    copy to clipboard
    (module mod1 br
      (define foo "zim")
      (provide foo))
    (require (submod "." mod1))

    (module mod2 br
      (define foo "zam")
      (provide foo))
    (require (submod "." mod2))
    ;; error: identifier `foo` imported twice
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    (module mod1 br
      (define foo "zim")
      (provide foo))
    (require (submod "." mod1))
    
    (module mod2 br
      (define foo "zam")
      (provide foo))
    (require (submod "." mod2))
    ;; error: identifier `foo` imported twice
    
    copy to clipboard
  3. (define-macro (define-as-42 ID)
      #'(define ID 42))
    (define-as-42 foo)
    foo ; 42

    (define-macro (define-bar-as-42)
      (with-pattern ([BAR-ID (datum->syntax caller-stx 'bar)])
        #'(define BAR-ID 42)))
    (define-bar-as-42)
    bar ; 42
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    (define-macro (define-as-42 ID)
      #'(define ID 42))
    (define-as-42 foo)
    foo ; 42
    
    (define-macro (define-bar-as-42)
      (with-pattern ([BAR-ID (datum->syntax caller-stx 'bar)])
        #'(define BAR-ID 42)))
    (define-bar-as-42)
    bar ; 42
    
    copy to clipboard
  1. (define (get-bar)
      (define bar 42)
      (let ([bar 92])
        bar))

    (get-bar) ; 92
    bar ; error: unbound identifier
    1
    2
    3
    4
    5
    6
    7
    (define (get-bar)
      (define bar 42)
      (let ([bar 92])
        bar))
    
    (get-bar) ; 92
    bar ; error: unbound identifier
    
    copy to clipboard
  2. (define top 40)

    (define (f1)
      (let ()
        (let ()
          (let ()
            top))))
    (f1) ; 40

    (define (f2)
      (let ([top 41])
        (let ([top 42])
          (let ([top 43])
            top))))
    (f2) ; 43
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    13
    14
    15
    (define top 40)
    
    (define (f1)
      (let ()
        (let ()
          (let ()
            top))))
    (f1) ; 40
    
    (define (f2)
      (let ([top 41])
        (let ([top 42])
          (let ([top 43])
            top))))
    (f2) ; 43
    
    copy to clipboard
  3. (define-macro (make-macro-scoped-foo)
      #'(begin
          (define foo 42)
          (* foo 2)))

    (make-macro-scoped-foo) ; 84
    foo ; unbound identifier (!)
    1
    2
    3
    4
    5
    6
    7
    (define-macro (make-macro-scoped-foo)
      #'(begin
          (define foo 42)
          (* foo 2)))
    
    (make-macro-scoped-foo) ; 84
    foo ; unbound identifier (!)
    
    copy to clipboard
    (define-macro (bind-existing-identifier ID)
      #'(begin
          (define ID 42)
          (* ID 2)))

    (bind-existing-identifier foo) ; 84
    foo ; 42 (and `foo` has module scope)

    (let ()
      (bind-existing-identifier bar)
      bar) ; 42 (and `bar` has expression scope)
    bar ; unbound identifier
     1
     2
     3
     4
     5
     6
     7
     8
     9
    10
    11
    12
    (define-macro (bind-existing-identifier ID)
      #'(begin
          (define ID 42)
          (* ID 2)))
    
    (bind-existing-identifier foo) ; 84
    foo ; 42 (and `foo` has module scope)
    
    (let ()
      (bind-existing-identifier bar)
      bar) ; 42 (and `bar` has expression scope)
    bar ; unbound identifier
    
    copy to clipboard
← prev next →