|
364 | 364 | (check-equal? (syntax->datum actual) 'FOO)) |
365 | 365 |
|
366 | 366 | (test-case "improper list with nested access" |
367 | | - ; #'(a b . (c FOO e)) flattens to (a b c FOO e) where c FOO e is the tail |
368 | | - ; But the tail is itself a syntax object wrapping (c FOO e) |
369 | | - ; So at index 2, we get the whole (c FOO e) syntax |
| 367 | + ; #'(a b . (c FOO e)) flattens to (a b (c FOO e)) |
| 368 | + ; The tail (c FOO e) is a syntax object at index 2 |
| 369 | + ; So at index 2, we get the whole (c FOO e) syntax, then index 1 within that gives FOO |
370 | 370 | (define stx #'(a b . (c FOO e))) |
371 | 371 | (define actual (syntax-ref stx (syntax-path (list 2 1)))) |
372 | 372 | (check-equal? (syntax->datum actual) 'FOO)) |
|
400 | 400 | (define unwrapped (syntax-e stx)) |
401 | 401 | (cond |
402 | 402 | ; Handle improper lists - flatten, update, and reconstruct |
| 403 | + ; Note: This does flatten/unflatten on each access, which could be optimized if needed |
| 404 | + ; for deeply nested paths, but improper lists are relatively rare in practice. |
403 | 405 | [(and (pair? unwrapped) (not (list? unwrapped))) |
404 | 406 | (define flattened (flatten-improper-list unwrapped)) |
405 | 407 | (define updated-elem (loop (list-ref flattened i) remaining-elements)) |
|
451 | 453 | (define (unflatten-improper-list flattened original) |
452 | 454 | (cond |
453 | 455 | [(null? flattened) '()] |
454 | | - [(null? (cdr flattened)) (car flattened)] |
455 | 456 | [(and (pair? original) (not (list? original))) |
456 | 457 | ; Reconstruct the improper structure |
457 | 458 | (let unflatten-with-tail ([flat flattened] [orig original]) |
458 | 459 | (cond |
459 | | - [(null? (cdr flat)) (car flat)] |
460 | | - [(pair? orig) |
461 | | - (cons (car flat) (unflatten-with-tail (cdr flat) (cdr orig)))] |
462 | | - [else (car flat)]))] |
463 | | - [else flattened])) |
| 460 | + [(or (null? (cdr flat)) (not (pair? orig))) |
| 461 | + ; Base case: last element or reached non-pair in original |
| 462 | + (car flat)] |
| 463 | + [else |
| 464 | + ; Recursive case: continue building the improper list |
| 465 | + (cons (car flat) (unflatten-with-tail (cdr flat) (cdr orig)))]))] |
| 466 | + [else |
| 467 | + ; For proper lists or single elements, return as-is |
| 468 | + flattened])) |
464 | 469 |
|
465 | 470 |
|
466 | 471 |
|
|
0 commit comments