PasteRack.org
Paste # 85169
2018-08-02 19:26:42

Fork as a new paste.

Paste viewed 65 times.


Embed:

#lang racket
#lang racket
(require (for-syntax racket/base syntax/parse))
(define-syntax (for/string stx)
  (syntax-parse stx 
    [(_for/string #:separator sep:expr . more)
     (syntax/loc stx
       (let ([s sep])
         (string-append*
          (append*
           (let ([xs (for/list ([x (for/list . more)])
                       (if (list? x) x (list x)))])
             (if s (add-between xs (list s)) xs))))))]
    [(_for/string . more)
     (syntax/loc stx
       (_for/string #:separator #f . more))]))

(for/string ([x '(1 2 3)])
  (~a x))

(for/string ([x '([a 1] [b 2] [c 3])])
  (map ~a x))

(for/string #:separator "\n"
  ([x '([a 1] [b 2] [c 3])])
  (map ~a x))

=>

prog:2:0: read: #lang not enabled in the current context
  context...:
   /home/pasterack/racket68/collects/syntax/module-reader.rkt:186:17: body
   /home/pasterack/racket68/collects/syntax/module-reader.rkt:183:2: wrap-internal
   /home/pasterack/racket68/collects/racket/../syntax/module-reader.rkt:65:9: lang:read-syntax
   /home/pasterack/racket68/share/pkgs/scribble-lib/scribble/private/manual-code.rkt:112:0: get-tokens
   /home/pasterack/racket68/share/pkgs/scribble-lib/scribble/private/manual-code.rkt:56:0: typeset-code15
   /home/pasterack/pasterack/tmp/85169/85169code.scrbl: [running body]
   loop
   ...cket/cmdline.rkt:179:51
   /home/pasterack/racket68/share/pkgs/scribble-lib/scribble/run.rkt: [running body]