开发者

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

开发者 https://www.devze.com 2023-04-02 07:51 出处:网络
(define-syntax prnt (syntax-rules () [(prnt elem ...) (display (format \"~a\" elem .开发者_JAVA百科..))]
(define-syntax prnt 
  (syntax-rules ()
               [(prnt elem ...) (display (format "~a" elem .开发者_JAVA百科..))]
               ))

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?


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))
0

精彩评论

暂无评论...
验证码 换一张
取 消