Skip to content

Instantly share code, notes, and snippets.

@ndpar
Last active August 29, 2015 14:06
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save ndpar/7c577fa0842c7cc2cdf6 to your computer and use it in GitHub Desktop.
Save ndpar/7c577fa0842c7cc2cdf6 to your computer and use it in GitHub Desktop.
Reverse zip
#lang racket
;; Inspired by SICP, chapter 5.2.2, and
;; https://www.youtube.com/watch?v=SrKj4hYic5A
(define (rzip c1 c2)
(let iter ([a c1] [z c2] [receive (λ (x y) y)])
(if (null? a)
(receive z '())
(iter (cdr a) z
(λ (y acc)
(receive (cdr y)
(cons (cons (car a) (car y))
acc)))))))
(module+ test
(require rackunit)
(check-equal? (rzip '(a b c) '(1 2 3))
'((a . 3) (b . 2) (c . 1))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment