这有效:
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) 当我尝试编译这段代码时
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) 我编译了(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) 自从matplotlib维护者最近离开后,我怀疑这个库是否仍然存在?我在它的主页上,但那里没有这样的信息.
何时以及如何在 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)
我的意思当然是应用程序运行时而不是初始化它们的源代码方式。
我有以下计划
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++ ×3
haskell ×2
syntax ×2
c ×1
c++11 ×1
compiler-bug ×1
constants ×1
lambda ×1
maintenance ×1
matplotlib ×1
operators ×1
pointers ×1
python ×1
variables ×1