方案lambda函数中的默认参数?
Default argument in scheme lambda function?
我最近开始使用 ChickenScheme,现在我想声明一个带有默认参数(如果未指定)的函数。
我在 Racket 网站上找到了这个例子,我知道 Racket 和 ChickenScheme 是不同的,但我认为这些基本的东西是相同的。
(define greet
(lambda (given [surname "Smith"])
(string-append "Hello, " given " " surname)))
这是来自 ChickenScheme 解释器的错误:
Error: during expansion of (lambda ...) - in `lambda' - lambda-list expected: (lambda (given (surname "Smith")) (string-append "Hello, " given " " surname))
Call history:
<syntax> (define greet (lambda (given (surname "Smith")) (string-append "Hello, " given " " surname)))
<syntax> (##core#set! greet (lambda (given (surname "Smith")) (string-append "Hello, " given " " surname)))
<syntax> (lambda (given (surname "Smith")) (string-append "Hello, " given " " surname)) <--
optional arguments in Racket is in fact non-standard, so don't expect other interpreters to implement it. In Scheme, the standard features are those defined in the RxRS Scheme reports, with R7RS being the latest one. But fear not - in Chicken Scheme, you can use optional
的语法:
[syntax] (optional ARGS DEFAULT)
Use this form for procedures that take a single optional argument. If ARGS is the empty list DEFAULT is evaluated and returned, otherwise the first element of the list ARGS. It is an error if ARGS contains more than one value.
这样使用:
(define (greet given . surname)
(string-append "Hello, " given " " (optional surname "Smith")))
在plain Scheme中,您可以使用dotted tail notation获取附加参数列表,然后检查该列表以查看是否提供了额外参数:
(define greet
(lambda (given . rest)
(let ((surname (if (pair? rest) (car rest) "Smith")))
(string-append "Hello, " given " " surname))))
因为这不是很方便,所以不同的Scheme系统为此提供了不同的替代方案。在 CHICKEN 中,我们支持 DSSSL annotations,因此您可以这样做:
(define greet
(lambda (given #!optional (surname "Smith"))
(string-append "Hello, " given " " surname)))
我最近开始使用 ChickenScheme,现在我想声明一个带有默认参数(如果未指定)的函数。 我在 Racket 网站上找到了这个例子,我知道 Racket 和 ChickenScheme 是不同的,但我认为这些基本的东西是相同的。
(define greet
(lambda (given [surname "Smith"])
(string-append "Hello, " given " " surname)))
这是来自 ChickenScheme 解释器的错误:
Error: during expansion of (lambda ...) - in `lambda' - lambda-list expected: (lambda (given (surname "Smith")) (string-append "Hello, " given " " surname))
Call history:
<syntax> (define greet (lambda (given (surname "Smith")) (string-append "Hello, " given " " surname)))
<syntax> (##core#set! greet (lambda (given (surname "Smith")) (string-append "Hello, " given " " surname)))
<syntax> (lambda (given (surname "Smith")) (string-append "Hello, " given " " surname)) <--
optional arguments in Racket is in fact non-standard, so don't expect other interpreters to implement it. In Scheme, the standard features are those defined in the RxRS Scheme reports, with R7RS being the latest one. But fear not - in Chicken Scheme, you can use optional
的语法:
[syntax] (optional ARGS DEFAULT)
Use this form for procedures that take a single optional argument. If ARGS is the empty list DEFAULT is evaluated and returned, otherwise the first element of the list ARGS. It is an error if ARGS contains more than one value.
这样使用:
(define (greet given . surname)
(string-append "Hello, " given " " (optional surname "Smith")))
在plain Scheme中,您可以使用dotted tail notation获取附加参数列表,然后检查该列表以查看是否提供了额外参数:
(define greet
(lambda (given . rest)
(let ((surname (if (pair? rest) (car rest) "Smith")))
(string-append "Hello, " given " " surname))))
因为这不是很方便,所以不同的Scheme系统为此提供了不同的替代方案。在 CHICKEN 中,我们支持 DSSSL annotations,因此您可以这样做:
(define greet
(lambda (given #!optional (surname "Smith"))
(string-append "Hello, " given " " surname)))