PasteRack.org
Paste # 17351
2015-08-29 14:58:17

Fork as a new paste.

Paste viewed 277 times.


Embed:

  1. #lang racket
  2. (require syntax/parse (for-syntax syntax/parse))
  3.  
  4. ;;this 'works', for a given definition of 'works'.
  5. (define-syntax (split/delim stx)
  6.   (syntax-case stx ()
  7.     [(_ delim stx0)
  8.      #'(syntax-parse stx0
  9.          [((~and (~not (~datum delim)) x) (... ...))
  10.           (list #'(x (... ...)))]
  11.          [((~and (~not (~datum delim)) x) (... ...) (~datum delim) xs (... ...))
  12.           (cons #'(x (... ...)) #'(xs (... ...)))])]))
  13.  
  14. ;;this consumes all available memory and CPU when applied.
  15. #;
  16. (define-syntax (split/delim stx)
  17.   (syntax-case stx ()
  18.     [(_ delim stx0)
  19.      #'(syntax-parse stx0
  20.          [((~and (~not (~datum delim)) x) (... ...))
  21.           (list #'(x (... ...)))]
  22.          [((~and (~not (~datum delim)) x) (... ...) (~datum delim) xs (... ...))
  23.           (cons #'(x (... ...)) (split/delim delim #'(xs (... ...))))])]))
  24.  
  25.  
  26. (split/delim |,| #'(1 + 1 |,| 5 + 1))
  27. (split/delim |,| #'(1 + 1))
  28. (split/delim |,| #'(1 + 1 |,| 3 * 5 |,| 25))

=>

'(#<syntax (1 + 1)> . #<syntax (5 + 1)>)

'(#<syntax (1 + 1)>)

'(#<syntax (1 + 1)> . #<syntax (3 * 5 |,| 25)>)