It's possible to create a ordered pair (cons in Lisp) using a lambda and a function, as shown in Use of lambda for cons/car/cdr definition in SICP
It also works in 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)
When I implement it in Rust, which is a statically-typed language:
fn cons(x: i32, y: i32) -> Box<Fn() -> Fn(i32, i32)> {
Box::new(move |m| m(x, y));
}
it shows the errors:
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 `()`
How do I define the type of m
?