user618815
user618815

Reputation:

How to write a macro that receives any number of arguments and print them out?

(define-syntax prnt 
  (syntax-rules ()
               [(prnt elem ...) (display (format "~a" elem ...))]
               ))

The above code run in racket will emit the following error:

format: format string requires 1 arguments, given 3; arguments were: "~a" "1" 2 3

then how can I achieve when use (prnt "1" 2 3), it can print any thing following prnt?

Upvotes: 2

Views: 410

Answers (1)

Matthias Benkard
Matthias Benkard

Reputation: 15769

If you really want a macro:

(define-syntax prnt 
  (syntax-rules ()
    [(prnt elem ...)
     (begin (displayln elem) ...)]))

You don't need a macro if all you want is being able to display multiple objects with a single function call, though:

(define (prnt . args)
  (for-each displayln args))

Upvotes: 2

Related Questions