gpt4 book ai didi

racket - 有没有办法将所有可选参数传递给另一个函数?

转载 作者:行者123 更新时间:2023-12-05 07:29:48 25 4
gpt4 key购买 nike

我有一个函数 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` -> 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

关于racket - 有没有办法将所有可选参数传递给另一个函数?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/52596785/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com