Racket 是否有方法将所有可选参数传递给另一个函数?
我有一个函数A,它接受10个可选参数。 我有另一个函数B,它接受相同的10个可选参数。Racket 是否有方法将所有可选参数传递给另一个函数?,racket,Racket,我有一个函数A,它接受10个可选参数。 我有另一个函数B,它接受相同的10个可选参数。 有没有简单的方法将一个函数的可选参数传递给另一个函数?解释器的解决方案 在解释器中,您只需执行以下操作: ; 0. define your arguments list which the functions have in common (define args-list '(1 2 3 4 5 6 7 8 9 10)) ; 1. call the functions `your-function` -&
有没有简单的方法将一个函数的可选参数传递给另一个函数?解释器的解决方案 在解释器中,您只需执行以下操作:
; 0. define your arguments list which the functions have in common
(define args-list '(1 2 3 4 5 6 7 8 9 10))
; 1. call the functions `your-function` -> your function name:
(eval `(your-function ,@args-list))
脚本解决方案
#lang racket
;; -------------------
;; prepare eval-call
;; -------------------
(define-namespace-anchor a)
(define ns (namespace-anchor->namespace a))
; this captures namespace for `eval` call in a script
;; -------------------
;; define macro for function call using an argument list
;; -------------------
(define-syntax-rule (fcall fun args)
(eval `(fun ,@args) ns))
;; -------------------
;; define two silly example functions with 10 optional arguments
;; -------------------
(define (add-10 [a 0] [b 0] [c 0] [d 0] [e 0] [f 0] [g 0] [h 0] [i 0] [j 0])
(+ a b c d e f g h i j))
(define (mult-10 [a 1] [b 1] [c 1] [d 1] [e 1] [f 1] [g 1] [h 1] [i 1] [j 1])
(* a b c d e f g h i j))
;; -------------------
;; define two example arguments lists
;; -------------------
(define args-list '(1 2 3 4 5 6 7 8 9 10))
(define args-list1 '(5 5 5))
;; -------------------
;; call the two different functions using the same argument lists
;; -------------------
(fcall add-10 args-list) ;; => 55
(fcall mult-10 args-list) ;; => 3628800
(fcall add-10 args-list1) ;; => 15
(fcall mult-10 args-list1) ;; => 125
;; in the interpreter, you can also directly call:
;(eval `(add-10 ,@args-list))
;(eval `(mult-10 ,@args-list))
;
;(eval `(add-10 ,@args-list1))
;(eval `(mult-10 ,@args-list1))
;; no namespace argument needed in interpreter, when calling `eval`
使用可选关键字参数列表编写脚本的解决方案
; now, let's do it for keyword arguments lists
; with optional keyword arguments
;; -------------------
;; define two silly example functions with 10 optional keyword arguments
;; -------------------
(define (add-it-10 #:a [a 0] #:b [b 0] #:c [c 0] #:d [d 0] #:e [e 0]
#:f [f 0] #:g [g 0] #:h [h 0] #:i [i 0] #:j [j 0])
(+ a b c d e f g h i j))
(define (mult-it-10 #:a [a 1] #:b [b 1] #:c [c 1] #:d [d 1] #:e [e 1]
#:f [f 1] #:g [g 1] #:h [h 1] #:i [i 1] #:j [j 1])
(* a b c d e f g h i j))
;; -------------------
;; let's define an example arguments list with keywords
;; -------------------
(define args-list2 '(#:a 1 #:b 2 #:c 3 #:d 4 #:g 10))
;; -------------------
;; call the two different functions using the same argument list
;; -------------------
(fcall add-it-10 args-list2) ;; => 20
(fcall mult-it-10 args-list2) ;; => 240
; ;also thinkable definition of `fcall`:
;(define-syntax-rule (fcall fun args)
; (apply fun args))
; note, this definition doesn't work with keyword argument lists!
; that is why the combination of eval and splicing of argument list
; is in my view the best
你是如何进入有10个位置参数的情况的?我想这可能是一个很好的例子,所以你能告诉我更多关于函数的内容和它们的作用吗?用许多漂亮的可选参数围绕(子进程…)函数做包装器。然后,在多参数函数(这是一种特殊情况)周围做一个包装器。您需要向我们展示您想到的特定代码序列,以及在什么情况下使用它。