是否可以对Lisp系列语言实施自动巡检? [英] Is it possible to implement auto-currying to the Lisp-family languages?

查看:95
本文介绍了是否可以对Lisp系列语言实施自动巡检?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

也就是说,当您使用一个参数来调用具有大于1的arity的函数时,它应该而不是显示错误,而是咖喱该参数并返回具有减小的arity的结果函数。可以使用Lisp的宏吗?

That is, when you call a function with >1 arity with only one argument, it should, instead of displaying an error, curry that argument and return the resulting function with decreased arity. Is this possible to do using Lisp's macros?

推荐答案

在Scheme中,可以使用来处理函数咖喱过程:

In Scheme it's possible to curry a function using the curry procedure:

(define (add x y)
  (+ x y))

(add 1 2)           ; non-curried procedure call
(curry add)         ; curried procedure, expects two arguments
((curry add) 1)     ; curried procedure, expects one argument
(((curry add) 1) 2) ; curried procedure call

从Racket的文档


[curry]返回的过程是proc的咖喱版本。第一次应用结果过程时,除非为其提供可以接受的最大参数数目,否则结果将是一个接受其他参数的过程。

[curry] returns a procedure that is a curried version of proc. When the resulting procedure is first applied, unless it is given the maximum number of arguments that it can accept, the result is a procedure to accept additional arguments.

您可以轻松实现一个宏,该宏在定义新过程时会自动使用 curry ,例如:

You could easily implement a macro which automatically uses curry when defining new procedures, something like this:

(define-syntax define-curried
    (syntax-rules ()
      ((_ (f . a) body ...)
       (define f (curry (lambda a (begin body ...)))))))

现在以下$ add 的定义将被管理:

Now the following definition of add will be curried:

(define-curried (add a b)
  (+ a b))

add
> #<procedure:curried>

(add 1)
> #<procedure:curried>

((add 1) 2)
> 3

(add 1 2)
> 3

这篇关于是否可以对Lisp系列语言实施自动巡检?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆