Last active
December 20, 2015 15:28
-
-
Save danking/6153980 to your computer and use it in GitHub Desktop.
Define values which ignores excess values
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| #lang racket | |
| (define-syntax (my-define-values stx) | |
| (syntax-case stx () | |
| ((_ (ids ...) expression) | |
| (with-syntax (((indices ...) | |
| (datum->syntax stx | |
| (build-list (length | |
| (syntax->list #'(ids ...))) | |
| (lambda (n) n))))) | |
| #'(define-values (ids ...) | |
| (call-with-values | |
| (lambda () expression) | |
| (lambda args | |
| (values (list-ref args indices) ...)))))))) | |
| (my-define-values (a b c) (values 1 2 3 4)) | |
| (equal? a 1) | |
| (equal? b 2) | |
| (equal? c 3) | |
| (my-define-values (d) (values 'foo 'bar 'baz)) | |
| (equal? d 'foo) | |
| (define (foo a b) | |
| (values a b)) | |
| (my-define-values (result) (foo 1 2)) | |
| (equal? result 1) |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment