What is scheme's equivalent of tuple unpacking?

前端 未结 6 1685
说谎
说谎 2021-02-05 07:39

In Python, I can do something like this:

t = (1, 2)
a, b = t

...and a will be 1 and b will be 2. Suppose I have a list \'(1 2) in

6条回答
  •  青春惊慌失措
    2021-02-05 08:05

    The general term for what you're looking for (at least in Lisp-world) is destructuring and a macro that implements it is known as destructuring-bind. In Common Lisp, it works like this:

    (destructuring-bind (a b c) '(1 2 3)
      (list a b c)) ;; (1 2 3)
    

    it also works for multiple "levels" of nesting:

    (destructuring-bind (a (b c) d) '(1 (2 3) 4)
      (list a b c d)) ;; (1 2 3 4)
    

    It looks like there's a nice implementation of destructuring-bind as a scheme macro.

提交回复
热议问题