它可以创建一个有序对使用Lambda和功能(在Lisp的利弊),如图Use of lambda for cons/car/cdr definition in SICP如何返回接收lambda/function的匿名函数/ lambda?
它也可以在Python:
def cons(x,y):
return lambda m:m(x,y)
def car(z):
return z(lambda x, y: x)
def cdr(z):
return z(lambda x, y: y)
当我实现它的锈,这是一种静态类型语言:
fn cons(x: i32, y: i32) -> Box<Fn() -> Fn(i32, i32)> {
Box::new(move |m| m(x, y));
}
它显示了错误:
error: the type of this value must be known in this context
--> src/main.rs:2:23
|
2 | Box::new(move |m| m(x, y));
| ^^^^^^^
error[E0308]: mismatched types
--> src/main.rs:1:54
|
1 | fn cons(x: i32, y: i32) -> Box<Fn() -> Fn(i32, i32)> {
| ______________________________________________________^ starting here...
2 | | Box::new(move |m| m(x, y));
3 | | }
| |_^ ...ending here: expected box, found()
|
= note: expected type `Box<std::ops::Fn() -> std::ops::Fn(i32, i32) + 'static + 'static>`
= note: found type `()`
如何定义m
的类型?