# Why would \`second\` being unbound identifier inside \`syntax-\>datum\`?

**URL:** <https://racket.discourse.group/t/why-would-second-being-unbound-identifier-inside-syntax-datum/2497>\
**Category:** Questions & Answers\
**Tags:** question\
**Created:** [November 13, 2023, 10:15am UTC](https://racket.discourse.group/t/why-would-second-being-unbound-identifier-inside-syntax-datum/2497 "2023-11-13T10:15:04Z")\
**Posts on this page:** 4\
**Page:** 1

<div class="post-metadata">

**Author:** ![JohnSnow](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@JohnSnow](https://racket.discourse.group/u/JohnSnow)\
**Post date:** [November 13, 2023, 10:15am UTC](https://racket.discourse.group/t/why-would-second-being-unbound-identifier-inside-syntax-datum/2497/1 "2023-11-13T10:15:04Z")

</div>

In this program, `ok` and `ok2` only differs at `cadr` & `second`

```scheme
#lang racket
(second '(1 2 3))

(define-syntax (ok stx)
  (datum->syntax stx (let ([s (syntax->datum stx)])
    (cadr s))))

(ok 1 2 3)

(define-syntax (ok2 stx)
  (datum->syntax stx (let ([s (syntax->datum stx)])
   (second s))))

(ok2 1 2 3)

```

Run it will yield an error that `second` is `unbound` when defining of `ok2`

Why would it unbound?

It is clearly included with `#lang racket`.

```scheme
second: reference to an unbound identifier
  at phase: 1; the transformer environment
  context...:
  matching binding...:
  common scopes...: in: second

```

Is this a bug? I am am using 8.10 [cs].

---

<div class="post-metadata">

**Author:** ![shawnw](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/shawnw/32/1031_2.png) [@shawnw](https://racket.discourse.group/u/shawnw)\
**Post date:** [November 13, 2023, 11:30am UTC](https://racket.discourse.group/t/why-would-second-being-unbound-identifier-inside-syntax-datum/2497/2 "2023-11-13T11:30:33Z")

</div>

You need to add a `(require (for-syntax racket/list))` to get the definition of `second` at macro expansion time. See [Compile and Run-Time Phases](https://docs.racket-lang.org/guide/stx-phases.html) in the Racket Guide for details.

---

<div class="post-metadata">

**Author:** ![soegaard](https://yyz2.discourse-cdn.com/free1/user_avatar/racket.discourse.group/soegaard/32/19_2.png) [@soegaard](https://racket.discourse.group/u/soegaard)\
**Post date:** [November 13, 2023, 11:39am UTC](https://racket.discourse.group/t/why-would-second-being-unbound-identifier-inside-syntax-datum/2497/3 "2023-11-13T11:39:38Z")

</div>

The `#lang racket` languages does include `racket/list` but only at runtime (phase 0).  
At compile time (phase 1) the imports are from `#lang racket/base` which doesn't  
include `racket/list`.

The cure as @shawnw mentions is to import `racket/list` for compile time using  
`(require (for-syntax racket/list))`.

Note that `second` checks that the input is a list and `cadr` doesn't.  
Therefore it might be better to use `cadr` depending on context.

Bonus tip: If you require `syntax/stx` for compile time, you can use `stx-car` and `stx-cdr` which work on both lists and syntax objects representing lists.

---

<div class="post-metadata">

**Author:** ![JohnSnow](https://avatars.discourse-cdn.com/v4/letter/j/74df32/32.png) [@JohnSnow](https://racket.discourse.group/u/JohnSnow)\
**Post date:** [November 13, 2023, 11:45am UTC](https://racket.discourse.group/t/why-would-second-being-unbound-identifier-inside-syntax-datum/2497/4 "2023-11-13T11:45:16Z")

</div>

Thanks very for the explanations!

I didn't realize "compile and run-time differences" has this implication before.
