Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix unquote-splicing lists within quasiquote #219

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

shawwn
Copy link
Contributor

@shawwn shawwn commented Mar 18, 2021

  • 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
Copy link
Contributor Author

shawwn commented Mar 18, 2021

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