Skip to content

Fix unquote-splicing lists within quasiquote - #219

Open
shawwn wants to merge 1 commit into
sctb:masterfrom
shawwn:2021-03-18/unquote-many
Open

Fix unquote-splicing lists within quasiquote#219
shawwn wants to merge 1 commit into
sctb:masterfrom
shawwn:2021-03-18/unquote-many

Conversation

@shawwn

@shawwn shawwn commented Mar 18, 2021

Copy link
Copy Markdown
Contributor
  • The following expression now correctly returns '(x y z):
    (eval `(let (a 'y b 'z) `(x ,,@'(a b))))

(It was returning '(x y) since (unquote a b) was ignoring b.)

  • MAP now supports splicing. E.g.:
    > (define-global spliced (x)
        (if (hd? x '%splice) x
            (obj? x) `(%splice ,x)
          x))

    > (define-global flatten (l)
        (while (first obj? l)
          (set l (map spliced l)))
        l)
    > (flatten '((1 2) 3 (4 5 (6))))
    (1 2 3 4 5 6)

    > (define-global intersperse (a bs)
        (tl (map (fn (x) `(%splice (,a ,x))) bs)))
    > (apply cat (intersperse ", " '(foo bar baz)))
    "foo, bar, baz"

- The following expression now correctly returns '(x y z):

    (eval `(let (a 'y b 'z) `(x ,,@'(a b))))

  (It was returning '(x y) since (unquote a b) was ignoring b.)

- MAP now supports splicing. E.g.:

    > (define-global spliced (x)
        (if (hd? x '%splice) x
            (obj? x) `(%splice ,x)
          x))

    > (define-global flatten (l)
        (while (first obj? l)
          (set l (map spliced l)))
        l)
    > (flatten '((1 2) 3 (4 5 (6))))
    (1 2 3 4 5 6)

    > (define-global intersperse (a bs)
        (tl (map (fn (x) `(%splice (,a ,x))) bs)))
    > (apply cat (intersperse ", " '(foo bar baz)))
    "foo, bar, baz"
@shawwn

shawwn commented Mar 18, 2021

Copy link
Copy Markdown
Contributor Author

I'm not sure this PR is worthwhile, but the root problem was that I couldn't implement pg's continuation-passing macros:

image

Specifically, the ,,@parms was silently truncating parms to its first value. E.g. if parms was (x y z), then ,,@parms was giving (unquote x), which caused some very strange bugs.

This unquote problem is also present in arc (but interestingly, common lisp handles it) so perhaps this "bug" could be considered a feature for simplicity.

The %splice trick in map was an interesting way to solve this. Once it was added, the fix was two lines of code. The tradeoff is that map might become very slightly slower, which may or may not be an acceptable tradeoff.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

1 participant