我正在进行的练习下面有一段代码片段.它读取CSV并将其输入链接列表然后打印到控制台.CSV看起来像这样:
5,3,19
7,12,2
13,15,25
22,0,7
Run Code Online (Sandbox Code Playgroud)
它在Linux和Windows中使用Visual Studio 2010和G ++进行编译.二进制文件在Windows XP命令提示符下执行,但在Git Bash(Windows XP)和Linux下运行时会发生分段错误.使用调试器(在Linux下)我已将问题隔离到printList()无法识别链表的末尾.
为什么会发生这种情况,我该怎么做才能阻止它?任何建议将不胜感激.
#include <cstdlib>
#include <sstream>
#include <iostream>
#include <fstream>
using namespace std;
// CSV source file parameters
const char *cSourceCSV = "source.csv";
const int iFieldsPerRow = 3;
enum direction_t {UP=1, STATIONARY=0, DOWN=-1};
// struct to hold data in a singly linked list
struct CallList {
float fTime; // time of call in seconds from beginning
int iFromPos;
int iToPos;
direction_t d_tDirectionWanted();
CallList *next;
};
direction_t CallList::d_tDirectionWanted() …Run Code Online (Sandbox Code Playgroud) 我在文件中有以下内容:
import Control.Monad
ema a = scanl1 $ \m n -> (1-a)*m + a*n
macd = ema 9 . uncurry (zipWith (-)) . liftM2 (,) (ema 26) (ema 12)
Run Code Online (Sandbox Code Playgroud)
在编译时,我得到以下内容:
:t macd
macd :: [Integer] -> [Integer]
Run Code Online (Sandbox Code Playgroud)
然而,
:t ema 9 . uncurry (zipWith (-)) . liftM2 (,) (ema 26) (ema 12)
ema 9 . uncurry (zipWith (-)) . liftM2 (,) (ema 26) (ema 12)
:: Num a => [a] -> [a]
Run Code Online (Sandbox Code Playgroud)
那么,为什么在更受限制的类型中存在差异macd呢?
我正在尝试理解Haskell中的Monads,在我无数次使用代码的实验中遇到过这样的事情:
f2 = do
return "da"
Run Code Online (Sandbox Code Playgroud)
并且它不想编译有关类型的巨大错误.我认为唯一重要的部分是:
No instance for (Monad m0) arising from a use of return'
The type variable `m0' is ambiguous
Run Code Online (Sandbox Code Playgroud)
那么我已将我的代码更改为:
f2 = do
return "da" :: IO [Char]
Run Code Online (Sandbox Code Playgroud)
而且效果非常好.但是当我试图弄乱一点并将类型更改为IO Int时再次出现错误.那么为什么这种类型实际上不是"含糊不清"?当我在返回之前添加一些内容时:
f2 = do
putStrLn "das"
return 2
Run Code Online (Sandbox Code Playgroud)
然后我不必指定返回的类型.那么有人可以解释我到底发生了什么吗?另外为什么在第一种情况下返回输出"da"?不是没有""?
我正在构建代码以获得理解,实际上是Solitaire求解器.我有一个简单的暴力实现,它使用状态monad,实际上只是为了证明我可以使用它(它只保留每个移动的计数).但是现在我想使用Unboxed Mutable数组来记录被访问的板,从而在我到达已经通过另一条路径访问的板位置时快速评估路径.似乎ST monad不允许我线程隐式状态,但是我必须使用ST(或IO)才能访问Mutable数组.因此,似乎我必须结合两个Monads - State来线程化状态(实际上包括一个Mutable数组),另一个(ST)来获得Mutable数组函数.
我在c ++中使用数组创建一个素数生成器,以便在找到它们时存储素数,然后使用它们来检查以后的电位.当我的程序继续时,有没有办法"增长"我的阵列?
如何在列表列表中获得最短列表?
我有
shortest :: [[a]] -> [a]
shortest [] = []
Run Code Online (Sandbox Code Playgroud)
我真的不知道从那里去哪里说实话感谢任何帮助
我试图解决H99中的一个问题:将列表分成两部分; 给出了第一部分的长度.
不要使用任何预定义的谓词.
例:
> (split '(a b c d e f g h i k) 3)
( (A B C) (D E F G H I K))
Run Code Online (Sandbox Code Playgroud)
我可以快速找到解决方案:
split'::[a]->Int->Int->[a]->[[a]]
split' [] _ _ _ = []
split' (x:xs) y z w = if y == z then [w,xs] else split' xs y (z+1) (w++[x])
split::[a]->Int->[[a]]
split x y = split' x y 0 []
Run Code Online (Sandbox Code Playgroud)
我的问题是我正在做的只是以递归格式重写循环版本.这是你在Haskell做事的正确方法吗?这不是命令式编程吗?
编辑:另外,你如何在这里避免额外的功能?
我正在开发一个C++的小项目,它要求我创建一个我在另一个类中编写的自定义类的对象.该类被调用FIRFilterModule,它有一个简单的空白构造函数.
作为一个java背景,我的冲动是创建它像这样:
class SensorInput{
public:
FIRFilterModule firFilter;
...More Class Members...
SensorInput():firFilter(FIRFilterModule()){}
...};
Run Code Online (Sandbox Code Playgroud)
但是,这会编译一个非常有用的错误消息"在此上下文中出错".我有点失落,为什么不起作用.增加我的困惑我将代码更改为:
class SensorInput{
public:
FIRFilterModule firFilter;
...More Class Members...
SensorInput(){}
...};
Run Code Online (Sandbox Code Playgroud)
有用.
有人能帮助我理解为什么会这样吗?
从绩效角度来看,还有更好的方法吗?
例如,创建一个名为arraydata的类/结构,它分配一些对齐的内存供使用(尽管由.dataPtr提供的指针):
class arraydata//to allocate some memory,
//and return a pointer to that block of memory
{
void *dataPtrV;
public:
double *dataPtr;
arraydata(int a, int b)
{
dataPtrV=_aligned_malloc(a*b*sizeof(double),32);
dataPtr=(double *)dataPtrV;
}
~arraydata()
{
_aligned_free(dataPtrV);
dataPtrV=NULL;
dataPtr=NULL;
}
};
Run Code Online (Sandbox Code Playgroud)
然后叫它:
arraydata X(30,20);
Run Code Online (Sandbox Code Playgroud) #include<stdio.h>
int main(){
int integers[40] = {1,2,3,4,5,6,7,8,9,10,-1};
int integers1[40];
int *ptr;
int *ptr1;
ptr = integers;
ptr1 = integers1;
while(*ptr != -1){
*ptr1++ = *ptr++;
}
*ptr1 = -1;
ptr1 = integers1;
while(*ptr1 != -1){
printf("\n *ptr1 : %d \n",*ptr1);
*ptr1++;
}
return 0;
}
Run Code Online (Sandbox Code Playgroud)
有没有办法在不使用循环的情况下一次打印ptr1指向的整数?
编辑:
#include<stdio.h>
int main(){
char integers[40] = {'1','2','3','4','5','6','7','8','9','10','\0'};
char integers1[40];
char *ptr;
char *ptr1;
ptr = integers;
ptr1 = integers1;
while(*ptr != '\0'){
*ptr1++ = *ptr++;
}
*ptr1 = -1;
ptr1 = integers1;
ptr …Run Code Online (Sandbox Code Playgroud) if ( $_ =~ /^(\d+)_[^,]+,"",(.+)"NR"(.+)"0","",""/ )
{ }
elsif ( $_ =~ /^[^_]+_[^,]+,"([\d\/]+)","[^"]+","[^"]+","[^"]+","[^"]+","[^"]+",
"[^"]+","[^"]+","[^"]+","[^"]+","[^"]+","[^"]+","[^"]+",.+/x )
Run Code Online (Sandbox Code Playgroud)
在第一次,是重复数字一次或多次,然后_,然后重复任何不等于,一次或多次的字符,什么,"",做什么?它看起来是一个空格还是逗号是某种类型的转义字符,有点混乱,没有能力在这台机器上测试它.正则表达式中通常有逗号吗?^一开始,它是一个锚或否定整个事物?
第二个声明更糟糕
我的代码接收十六进制的值列表,我必须将它们传递给二进制文件并将每个结果放在一个列表中,但我有这两个错误,我不知道如何解决它们
Pixels.hs:121:29:
Occurs check: cannot construct the infinite type:
t0 = Bool -> [a1] -> t0
In the return type of a call of `modA'
Probable cause: `modA' is applied to too many arguments
In the expression:
modA (o ++ [(k `mod` 2)]) (l + 1) (k `div` 2) otherwise o
In an equation for `modA':
modA o l k
| l < 8 = modA (o ++ [(k `mod` 2)]) (l + 1) (k `div` 2) otherwise o
Pixels.hs:126:89: …Run Code Online (Sandbox Code Playgroud)