我想说我想用咖喱语法定义像format
这样的函数。我如何处理可以传递的其他参数?例如,我希望能够执行以下操作:
(((format "~a ~a") 5) 9)
答案 0 :(得分:1)
format
应该如何知道它有所有的论据。我猜你希望该格式将解析其输出字符串并确定它需要多少个参数,然后在收到正确数量的参数时触发。你最终会自己滚动它。像这样的东西(注意:未经编辑的代码):
#lang racket
;; given a format-string, return a curried function that
;; accepts the format-string's arguments one at a time
(define (curried-format fmt-str)
(define num-args-expected (count-format-string-args fmt-str))
(define (so-far->waiter so-far)
(lambda (arg)
(define new-args (cons arg so-far))
;; do we have all of the args?
(if (= (length new-args) num-args-expected)
;; yay! call format:
(apply format fmt-str (reverse new-args))
;; nope: wait for some more:
(so-far->waiter new-args))))
(so-far->waiter '()))