我想写这样的函数:
(define foo ( (a b c) (+ a (+ b c))))
将其自动转换为:
(define foo (lambda (a) (lambda (b) (lambda (c) (+ a (+ b c))))))
并像这样使用(如果可能的话(:
(map (foo 1 2) (interval 1 10))
就好像我在写这个:
(map ((foo 1) 2) (interval 1 10))
我不知道如何在scheme中编写宏,但我需要编写一个函数来转换引用的表达式
(f arg1 arg2 argn)
像这样:
(define-macro clambda ;curried lambda
(lambda xs
(if (< (length xs) 2)
(if (eq? 1 (length xs))
(lambda () xs)
(lambda (head xs) (tail xs)))
(lambda (head xs) (clambda (tail xs))))))
我该怎么做?
以下是我对您的宏的建议:
(define-syntax
(syntax-rules ()
[(_ "build" (a) body)
(lambda (a . rest)
(if (null? rest)
body
(error "Too many arguments")))]
[(_ "build" (a b ...) body)
(lambda (a . rest)
(define impl ( "build" (b ...) body))
(if (null? rest)
impl
(apply impl rest)))]
[(_ (a b ...) body)
( "build" (a b ...) body)]
[(_ . rest) (error "Wong use of \")]))
(define test ( (a b c) (+ a b c)))
(define partial (test 4 5))
(partial 6) ; ==> 15
这确实使生成的代码有更多的开销,因为如果每个lambda得到更多的参数,它将应用下一个lambda。如果你传递了太多的论点,它也会产生错误,因为否则你会得到不清楚的";申请,而不是程序";
您可能需要实施error
。