小编W. *_*Zhu的帖子

为什么foldl'使用大量RAM且数据结构复杂?

惰性折叠使用大量 RAM。在 中Data.Listfoldl'提供了使用严格评估的左折叠。例如,以下计算 1000 万个零的总和,而 RAM 使用量几乎没有增加。

sum0 = foldl' (+) 0 (replicate 10000000 0)
Run Code Online (Sandbox Code Playgroud)

然而,这似乎不适用于复杂的数据结构。例如,如果我们定义数字对的加法并计算零对的总和,则 RAM 使用量会显着增加:

(x1,y1) <+> (x2,y2) = (x1 + x2,y1 + y2)
sum00 = foldl' (<+>) (0,0) (replicate 10000000 (0,0))
Run Code Online (Sandbox Code Playgroud)

为什么会发生这种情况?有没有办法减少 RAM 使用量?

haskell fold

17
推荐指数
1
解决办法
739
查看次数

指向整数数组的指针数组

我只是想知道是否有一种方法可以使指针数组指向多维数组整数中每行的第一列.例如,请查看以下代码:

#include <stdio.h>

int day_of_year(int year, int month, int day);

main()
{
    printf("Day of year = %d\n", day_of_year(2016, 2, 1));
    return 0;
}

static int daytab[2][13] = {
    {0, 31, 28, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}, 
    {0, 31, 29, 31, 30, 31, 30, 31, 31, 30, 31, 30, 31}
};

int day_of_year(int year, int month, int day)
{
    int leap;
    int *nlptr = &daytab[0][0];
    int *lpptr = &daytab[1][0];
    int *nlend = nlptr + month;
    int …
Run Code Online (Sandbox Code Playgroud)

c arrays pointers turbo-c

8
推荐指数
1
解决办法
104
查看次数

为什么gdb不会在strcpy上中断?

请查看下面的命令行.我设置了两个断点:一个在strcpy上,另一个在printf上.为什么跳过断点1?

root@ninja:~/Desktop/Programs# gcc -g -o exp exp.c
root@ninja:~/Desktop/Programs# gdb -q exp
Reading symbols from /root/Desktop/Programs/exp...done.
(gdb) list
1   #include <stdio.h>
2   #include <string.h>
3   
4   int main()  {
5       char str_a[20];
6       
7       strcpy(str_a, "Hello world!\n");
8       printf(str_a);
9   }
(gdb) break strcpy
Function "strcpy" not defined.
Make breakpoint pending on future shared library load? (y or [n]) y
Breakpoint 1 (strcpy) pending.
(gdb) break printf
Breakpoint 2 at 0x8048300
(gdb) run
Starting program: /root/Desktop/Programs/exp 

Breakpoint 2, 0xb7eabf54 in printf …
Run Code Online (Sandbox Code Playgroud)

c gdb breakpoints

6
推荐指数
1
解决办法
972
查看次数

getline中第二个参数的作用是什么?

#include <stdio.h>
#include <stdlib.h>

int main(void)
{
    char* buffer = malloc(100 * sizeof(char));
    size_t n = 3;

    getline(&buffer, &n, stdin);
    printf("%s\n", buffer);
    free(buffer);
}
Run Code Online (Sandbox Code Playgroud)

getline我认为,中的第二个参数size_t *n是限制读取的字符数。但是当我尝试使用更大的输入时,它仍然读取所有输入。我在手册页和网上进行了搜索,但找不到答案。有人能为我解释一下吗?

c getline

4
推荐指数
1
解决办法
1156
查看次数

如何将 Pair 定义为 Monoid?

我的数据类型Pair定义为

data Pair a b = Pair a b
Run Code Online (Sandbox Code Playgroud)

我想让它成为幺半群。这是我的定义。

instance (Monoid a,Monoid b) => Monoid (Pair a b) where
    mempty = Pair mempty mempty
    mappend (Pair x1 y1) (Pair x2 y2) = Pair (mappend x1 x2) (mappend y1 y2)
Run Code Online (Sandbox Code Playgroud)

但是,我收到以下错误。

foldable.hs:3:10: error:
    • Could not deduce (Semigroup (Pair a b))
        arising from the superclasses of an instance declaration
      from the context: (Monoid a, Monoid b)
        bound by the instance declaration at foldable.hs:3:10-49
    • In the instance declaration …
Run Code Online (Sandbox Code Playgroud)

haskell monoids

3
推荐指数
1
解决办法
115
查看次数

标签 统计

c ×3

haskell ×2

arrays ×1

breakpoints ×1

fold ×1

gdb ×1

getline ×1

monoids ×1

pointers ×1

turbo-c ×1