我正在尝试在Rust中进行一些高阶编程,但是我在处理闭包时遇到了一些困难.这是一个代码片段,说明了我遇到的一个问题:
pub enum Foo {
Bar(Box<FnOnce(i32)>),
}
pub fn app(i: i32, arg: Foo) {
match arg {
Foo::Bar(f) => f(i),
}
}
Run Code Online (Sandbox Code Playgroud)
当我编译这段代码时,我收到以下错误消息:
error[E0161]: cannot move a value of type std::ops::FnOnce(i32) + 'static: the size of std::ops::FnOnce(i32) + 'static cannot be statically determined
--> src/main.rs:7:24
|
7 | Foo::Bar(f) => f(i),
| ^
Run Code Online (Sandbox Code Playgroud)
因为我把函数放在一个Box,我会想到这将解决编译器不知道大小的问题.如何编译上述程序?
我的问题很简单,因为任何人开始使用haskell我一直在考虑类型,功能组成以及如何应用它们.我开始考虑((+) . (*))可能的结果.
现在很明显,这个问题的解决方案是开放ghci并找出答案.所以我这样做并检查了类型:
?> :t ((*) . (+))
((*) . (+)) :: (Num (a -> a), Num a) => a -> (a -> a) -> a -> a
Run Code Online (Sandbox Code Playgroud)
这种类型可能吗?我很难理解它可能是什么或它意味着什么?
再次为这个简单化的问题道歉,我试图加入到函数中的一切都失败了.我只是试图通过二元函数来开发函数组合的直觉.
在构建Haskell程序和库时,我一直在使用沙箱.但偶尔,我会构建一个我想在系统范围内安装的程序.似乎没有一种简单的方法来获取一个内置在沙盒中并将其安装在沙箱之外的程序.
我有一个关于malloc()的问题.真奇怪.我的代码如下.我使用随机生成器为数组生成元素.该数组由malloc()打开.如果数组大小小于8192,则可以.如果大小大于8192,则显示段故障.
void random_generator(int num, int * array) {
srand((unsigned)time(0));
int random_integer;
for(int index=0; index< num; index++){
random_integer = (rand()%10000)+1;
*(array+index) = random_integer;
cout << index << endl;
}
}
int main() {
int array_size = 10000;
int *input_array;
input_array = (int*) malloc((array_size));
random_generator(8192, input_array); // if the number is larger than 8192, segment fault
free(input_array);
}
Run Code Online (Sandbox Code Playgroud) 我有以下问题.我有类型的价值,(forall r. MyType r)我需要ParsecT s u m (forall r. MyType r).是否可以在不提供额外data结构的情况下完成?