Skip to content

Instantly share code, notes, and snippets.

@Heimdell
Last active August 29, 2015 14:16
Show Gist options
  • Save Heimdell/a13d9d125eb9c12c1b36 to your computer and use it in GitHub Desktop.
Save Heimdell/a13d9d125eb9c12c1b36 to your computer and use it in GitHub Desktop.
#lang racket
(define-syntax-rule (-> args . body) (lambda args . body))
(define atom? (or/c number? symbol? boolean? string?))
(define (transform body return)
(trace 'transform-2 body (-> ()
(if (atom? body)
(return body)
(split-by '-> body
(-> ()
(map-cont transform body return))
(-> (args body)
(map-cont transform body (-> (body)
(return `(lambda ,args ,@body)))))
)))))
(define (map-cont f list return)
(define (map-cont-aux acc list)
(uncons list
(-> (head tail)
(f head (-> (new-head)
(map-cont-aux (cons new-head acc) tail))))
(-> ()
(return (reverse acc)))))
(map-cont-aux '() list))
(define (uncons list on-cons on-nil)
(if (null? list)
(on-nil)
(on-cons (car list) (cdr list))))
(define (split-by sep list on-not-found on-found)
(uncons list
(-> (head tail)
(if (equal? sep head)
(on-found '() tail)
(split-by sep tail on-not-found (-> (before after)
(on-found (cons head before) after)))))
on-not-found))
(define (trace prefix value later)
(begin (print-all prefix value))
(later))
(define (print-all . all)
(print all))
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment