PasteRack.org
Paste # 20019
2018-08-02 19:27:07

Fork as a new paste.

Paste viewed 296 times.


Embed:

  1. #lang racket
  2. (require (for-syntax racket/base syntax/parse))
  3. (define-syntax (for/string stx)
  4.   (syntax-parse stx
  5.     [(_for/string #:separator sep:expr . more)
  6.      (syntax/loc stx
  7.        (let ([s sep])
  8.          (string-append*
  9.           (append*
  10.            (let ([xs (for/list ([x (for/list . more)])
  11.                        (if (list? x) x (list x)))])
  12.              (if s (add-between xs (list s)) xs))))))]
  13.     [(_for/string . more)
  14.      (syntax/loc stx
  15.        (_for/string #:separator #f . more))]))
  16.  
  17. (for/string ([x '(1 2 3)])
  18.   (~a x))
  19.  
  20. (for/string ([x '([a 1] [b 2] [c 3])])
  21.   (map ~a x))
  22.  
  23. (for/string #:separator "\n"
  24.   ([x '([a 1] [b 2] [c 3])])
  25.   (map ~a x))

=>

"123"

"a1b2c3"

"a1\nb2\nc3"