I've got problem with a Haskell program.
I'm trying to change [[Char]] to [[Int]]
I've got
["2","2","1","2,2","1"]
Run Code Online (Sandbox Code Playgroud)
list of char list
and I'm trying to change it to [[Int]]
[[2],[2],[1],[2,2],[1]]
Run Code Online (Sandbox Code Playgroud)
I've tried
f :: [String] -> [Int]
f = map read
Run Code Online (Sandbox Code Playgroud)
but it gives me
[2,2,1,*** Exception: Prelude.read: no parse
Can anybody help me with this?
我有 MPI 库的问题。我必须从文件中读取文本并将其发送到另一个进程,例如作为向量。
我编写了以下代码:
#include "mpi.h"
#include <stdio.h>
#include<string.h>
#include<stdlib.h>
#include<string>
#include <fstream>
#include <cstring>
#include <vector>
class PatternAndText
{
public:
static std::string textPreparaation()
{
std::ifstream t("file.txt");
std::string str((std::istreambuf_iterator<char>(t)), std::istreambuf_iterator<char>());
std::string text = str;
return text;
}
};
int main(int argc, char* argv[])
{
int size, rank ;
std::string text;
std::vector<char> cstr;
MPI_Init(&argc, &argv);
MPI_Status status;
MPI_Comm_rank(MPI_COMM_WORLD, &rank);
MPI_Comm_size(MPI_COMM_WORLD, &size);
if (rank == 0)
{
text = PatternAndText::textPreparaation();
std::vector<char> cstr(text.c_str(), text.c_str() + text.size() + 1);
}
MPI_Bcast(cstr.data(), cstr.size(), MPI_CHAR,0,MPI_COMM_WORLD);
if …Run Code Online (Sandbox Code Playgroud) 我上了一堂简短的课Car:
class Car:
def __init__(self, brand, model, color, accesories):
self.brand = brand
self.model = model
self.color = color
self.accesories = ['radio']
def __str__(self):
return " accessories {}".format(self.accesories)
def __iadd__(self, other):
self.accesories.extend(other)
print(self.accesories)
return Car(self.brand, self.model, self.color, self.accesories)
Run Code Online (Sandbox Code Playgroud)
我创建一个对象:
car1 = Car('opel','astra','blue',[])
Run Code Online (Sandbox Code Playgroud)
当我尝试通过以下方式添加其他附件时:
car1 += ['wheel']
Run Code Online (Sandbox Code Playgroud)
它打印:
class Car:
def __init__(self, brand, model, color, accesories):
self.brand = brand
self.model = model
self.color = color
self.accesories = ['radio']
def __str__(self):
return " accessories {}".format(self.accesories)
def __iadd__(self, other):
self.accesories.extend(other)
print(self.accesories)
return …Run Code Online (Sandbox Code Playgroud) 我对haskell功能有问题。我想从列表或列表[[Int]]中删除特定元素
有很多简单的方法可以删除列表[Int]中的元素
例如
removeItem :: Int -> [Int] -> [Int]
removeItem _ [] = []
removeItem x (y:ys) | x == y = removeItem x ys
| otherwise = y : removeItem x ys
Run Code Online (Sandbox Code Playgroud)
但我不能写
removeItem :: Int -> [[Int]] -> [[Int]]
Run Code Online (Sandbox Code Playgroud)
它应该给出一个结果:
removeItem 1 [[1],[2,2],[3],[1]]
Run Code Online (Sandbox Code Playgroud)[[],[2,2],[3],[]]
有谁能够帮助我