小编Tri*_*tos的帖子

为什么这个功能组合是非法的

这有效:

c <- fmap lines (readFile "d:\\tmp\\h.txt")  
let h = map (read :: String -> Int) c 
Run Code Online (Sandbox Code Playgroud)

而那些"不能编译"的那两行的"叠加"

fmap (read :: String -> Int) $ fmap lines (readFile "d:\\tmp\\h.txt")

它会产生错误:

interactive:1:36:
    Couldn't match expected type `Char' with actual type `[Char]'
    Expected type: String -> String
      Actual type: String -> [String]
    In the first argument of `fmap', namely `lines'
    In the second argument of `($)', namely
      `fmap lines (readFile "d:\\tmp\\h.txt")

为什么它不编译以及如何在一行中执行此操作?我想要的是实现python的简单性

[int(i) for i in open("d:\\tmp\\h.txt")]
Run Code Online (Sandbox Code Playgroud)

syntax haskell

2
推荐指数
3
解决办法
251
查看次数

无法从算术序列`(x - n + 1)... x'推断出(枚举a)

当我尝试编译这段代码时

prod [] = 1
prod (x:xs) = x * prod xs

ff :: (Num a) => a -> a -> a
ff x n = prod [(x - n + 1) .. x]
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

a.hs:5:15:
    Could not deduce (Enum a)
     arising from the arithmetic sequence `(x - n + 1) .. x'
    from the context (Num a)
      bound by the type signature for ff :: Num a => a -> a -> a
      at a.hs:5:1-32
    Possible fix:
      add (Enum …
Run Code Online (Sandbox Code Playgroud)

syntax haskell

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

为什么lambda捕获的这个指针被破坏而其他变量不被破坏

我编译了(g ++ -std = c ++ 11 a.cpp)并运行以下代码:

#include <iostream>
#include <functional>

using namespace std;

class A {
    std::function<void(void)> f;
public:
    A(std::function<void(void)> pf) : f(pf) {}
    void callf() { f(); }
};

class B {
    A *a;
public:
    void test() {
        B *that = this;
        auto f = [this, that]() {
            cout << "this: " << this << " that: " << that << endl;
            delete this->a;
            cout << "this: " << this << " that: " << that << …
Run Code Online (Sandbox Code Playgroud)

c++ lambda pointers c++11

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

matplotlib仍然保持

自从matplotlib维护者最近离开后,我怀疑这个库是否仍然存在?我在它的主页上,但那里没有这样的信息.

python maintenance matplotlib

0
推荐指数
1
解决办法
112
查看次数

const 变量何时初始化

何时以及如何在 C/C++ 中初始化 const 变量?我对特定类型很好奇:

1) const static member of a class
2) function const local variable
3) const global variable
Run Code Online (Sandbox Code Playgroud)

我的意思当然是应用程序运行时而不是初始化它们的源代码方式。

c c++ variables constants

0
推荐指数
1
解决办法
252
查看次数

C++比较运算符不返回true或false

我有以下计划

int a = 216;
bool* v = (bool*)((void*)&a);
std::cout << (*v == true) << endl;
Run Code Online (Sandbox Code Playgroud)

我希望这个程序打印出真或假,但它打印出216.我编译了它g++ (Ubuntu 4.8.2-19ubuntu1) 4.8.2.这是预期的行为还是一些错误?为什么等式运算符会返回与bool不同的类型?

---------编辑---------

我的目的不是为了使其无效,而是将v存储216存储在其存储位置.替代程序可能如下所示:

bool v;
int a = 216;
memcpy(&v, &a, sizeof(bool));
std::cout << (v == true) << endl;
Run Code Online (Sandbox Code Playgroud)

或者我可以使用未初始化的bool指针指向一些随机值,恰好是例如216.

c++ operators logical-operators compiler-bug

0
推荐指数
1
解决办法
130
查看次数