它可以創建一個有序對使用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
的類型?