Skip to content

Instantly share code, notes, and snippets.

@dgellow
Last active August 29, 2015 14:01
Show Gist options
  • Save dgellow/85a6c44375e2012ab6f1 to your computer and use it in GitHub Desktop.
Save dgellow/85a6c44375e2012ab6f1 to your computer and use it in GitHub Desktop.
A function to transform a list into a list of cons.
;; Example:
;; (part-cons '(1 2 3 4 5)) ;; => '((1 . 2) (3 . 4) (5))
(define (part-cons l [acc '()])
(if (equal? l null)
acc
(let ([k (car l)]
[v (if (null? (cdr l)) null (cadr l))]
[ll (if (> (length l) 1)
(drop l 2)
(drop l 1))])
(if (equal? acc null)
(part ll (list (cons k v)))
(part ll (append acc
(list (cons k v))))))))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment