What is scheme's equivalent of tuple unpacking

2019-03-18 03:44发布

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 Scheme. Is there any way to do something similar with let? If it makes a difference, I'm using Racket.

6条回答
我欲成王,谁敢阻挡
2楼-- · 2019-03-18 03:58

This works in Racket if you don't want to bring in the match dependency:

From a list:

(let-values ([(a b c) (apply values '(1 2 3))])
  (+ a b c))

Or directly from a values expression:

(let-values ([(a b c) (values 1 2 3)])
  (+ a b c))
查看更多
等我变得足够好
3楼-- · 2019-03-18 04:00

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.

查看更多
姐就是有狂的资本
4楼-- · 2019-03-18 04:01

In racket you can use match,

(define t (list 1 2))
(match [(list a b) (+ a b)])

and related things like match-define:

(match-define (list a b) (list 1 2))

and match-let

(match-let ([(list a b) t]) (+ a b))

That works for lists, vectors, structs, etc etc. For multiple values, you'd use define-values:

(define (t) (values 1 2))
(define-values (a b) (t))

or let-values. But note that I can't define t as a "tuple" since multiple values are not first class values in (most) scheme implementations.

查看更多
太酷不给撩
5楼-- · 2019-03-18 04:06

A bare-bones idiom is to use apply with lambda where you'd use let, like:

(define t '(1 2))
(apply (lambda (a b)
          ;; code that would go inside let
        )
        t)

The advantage is that it works on any implementation. Of course this can only be used on simple cases, but sometimes that's all you need.

查看更多
▲ chillily
6楼-- · 2019-03-18 04:15

I think this is what you are looking for:

Look at let-values or let+.

查看更多
做个烂人
7楼-- · 2019-03-18 04:21

Here is a simple destructuring-bind macro for schemes with case-lambda (such as Racket or Chez Scheme):

(define-syntax bind
   (syntax-rules ()
      ((_ arg pat def body)
         (apply
            (case-lambda
               [pat body]
               [x def] )
            arg ))))

Here is the example that motivated me to write this macro. Putting the default before the body makes for readable code:

(define (permutations l)
   ;
   (define (psub j k y)
      ;
      (define (join a b)
         (bind a (ah . at) b
            (join at (cons ah b)) ))
      ;
      (define (prec a b z)
         (bind b (bh . bt) z
            (prec (cons bh a) bt
               (psub (cons bh j) (join a bt) z) )))
      ;
      (if (null? k)
         (cons (reverse j) y)
         (prec (list) k y) ))
   ;
   (psub (list) (reverse l) (list)) )

Here are benchmarks for computing permutations of length 9, on various schemes:

0m0.211s Chez Scheme
0m0.273s Bigloo
0m0.403s Chicken
0m0.598s Racket

The translation to GHC Haskell is 5x faster than Chez Scheme. Guile is much slower than any of these schemes.

Aside from the ease of leveraging the existing case-lambda code, I like how this macro accepts exactly the same syntax as function definition argument lists. I love the simplicity of scheme. I'm old enough to remember programming Fortran on punched cards, where the allowed syntax varied wildly with context. Scheme is supposed to be better than that. The impulse is overwhelming to guild the lily on macros like this. If you can't justify changing the syntax for function definitions too, then don't change that syntax here either. Having an orthogonal grammar is important.

查看更多
登录 后发表回答