在c ++中有不同的方法来初始化变量.
int z(3)
与int相同z=3
.是
int z;
z(3);
Run Code Online (Sandbox Code Playgroud)
与...一样
int z;
z=3;
Run Code Online (Sandbox Code Playgroud)
?
我想知道是否可以将递归函数转换为无点定义.
如果我们采用简单的递归函数.
factorial :: int-> int
factorial 0=1
factorial n+1= (n+1) *factorial n
Run Code Online (Sandbox Code Playgroud)
如果我们有非递归def.
factorial :: int-> int
factorial n= product [1..n]
<=> factorial n = product.enumFromTo 1 n
<=> factorial = product.enumFromTo 1
Run Code Online (Sandbox Code Playgroud)
但是我怎么能在递归定义上做同样的事情呢?
我之所以要问的原因是我想要制作transformationsApply
无瑕疵.
transformationsApply :: Eq a => a -> ([a] -> [a]) -> [([a], [a])] -> [a] -> Maybe [a]
transformationsApply _ _ [] _= Nothing
transformationsApply wc func ((a,b):xs) (y:ys)
= orElse (transformationApply wc func (y:ys) (a,b))
(transformationsApply wc func xs (y:ys))
Run Code Online (Sandbox Code Playgroud)
transformationApply …
我有这个功能
rulesApply :: [PhrasePair] -> Phrase -> Phrase
rulesApply pp = try (transformationsApply "*" reflect pp )
Run Code Online (Sandbox Code Playgroud)
我想学习如何使它无点.
try :: (a -> Maybe a) -> a -> a
try f x = maybe x id (f x)
transformationsApply :: Eq a => a -> ([a] -> [a]) -> ([([a], [a])] -> ([a] -> Maybe [a]))
transformationsApply wc f pfPair list = foldr1 orElse (map (transformationApply wc f list) pfPair)
rulesApply pp = try (transformationsApply "*" reflect pp )
Run Code Online (Sandbox Code Playgroud)
(transformationsApply "*" …
表达式的类型和值是什么:
do [1,2,3]; "lambda"
Run Code Online (Sandbox Code Playgroud)
我测试了它,发现它只打印lambda 3次.但我不明白为什么会那样做.我怎么能用bind重写它.感觉有必要重写它.
c a = (a\\).(a\\)
Run Code Online (Sandbox Code Playgroud)
我知道(\\)= foldl (flip delete)
它是标准功能.
delete x [] = []
delete x (y:ys) = if x == y then ys else y : delete x ys
Run Code Online (Sandbox Code Playgroud)
我想知道我能找出什么功能c
.我该怎么办?如果我只关注(a\\)
删除需要列表和我们想要从列表中删除的斧头.
(a\\)
已经去了一个列表,所以我们需要发送一个我们想删除的值x.a=[a1,a2,a3]
foldl => (a1 'f' x) 'f' a2) 'f' a3
Run Code Online (Sandbox Code Playgroud)
哪里f
是flip delete
.
所以从a的第一个元素中删除x并从结果列表中删除子列表a2和a3.所以我们将获得a1独有的所有值.它似乎是一个列表列表.不知道这是否正确.
任何人都可以解释一下可变修改类型是什么吗?
如果我们有一个数组
a[n]
,并n
在编译时是不知道,然后a
是VLA.给定一个数组b[c][d]
,其中c
并d
不会知道,直到运行时暗示b
是VLA,对不对?
在我的书中,他们说可变修改类型包含VLA.而已; 而已.
如何创建指向可变修改类型的指针?
data Error a = OK a | Error String
instance Monad Error where
return = OK
Run Code Online (Sandbox Code Playgroud)
在这种情况下我应该如何定义bind?
>>= : Error a -> (a->error b) -> error b
对于这种情况,这是函数绑定的类型,我希望它是正确的.
OK a >>= f = (don't know where to begin need some help)
Error err >>=f = (in this case can i just return error like = Error err)
Run Code Online (Sandbox Code Playgroud)
我也想知道是否有人可以解释为什么这些seq的使用有问题.你可以在幻灯片上找到它们(页面)10 seq的用法 http://fileadmin.cs.lth.se/cs/Education/EDAN40/lectures/Parallelism.pdf hittendInside xy = someFunc(x'seq'y)为什么这是有问题的forexamble是因为seq不会返回x它只会评估它并且只会返回y.在其他两个考试中也是如此吗?
我有两个不同的相同功能的实现
IPAddress UMNS::lookup(const HostName& name) const{
auto it=ns.find(name);
if(it!=ns.end()){
return (*it).second;
}
return NON_EXISTING_ADDRESS;
}
Run Code Online (Sandbox Code Playgroud)
和
IPAddress UMNS::lookup(const HostName& name) const{
auto it=find_if(ns.begin(),ns.end(),
[&name] ( const pair<HostName,IPAddress> &a){ return a.first==name;});
bool found = it != ns.end();
if ( found ){
return (*it).second;
}
return NON_EXISTING_ADDRESS;
}
Run Code Online (Sandbox Code Playgroud)
ns是一个unordered_map
但两个函数的执行时间不一样.
第一个实现给出了这样的:
搜索次数:1000000
平均搜索时间(ms):0.000373
第二个实现给出了这个:
搜索次数:1000000
平均搜索时间(ms):24.9
第二次实施有什么问题?
为什么我不能使用find_if?
==3905== ERROR SUMMARY: 14 errors from 2 contexts (suppressed: 2 from 2)
==3905==
==3905== 6 errors in context 1 of 2:
==3905== Invalid write of size 4
==3905== at 0x401BFE: EliminateXr (in /home/suraj/Desktop/project/fm)
==3905== by 0x402040: fm_elim (in /home/suraj/Desktop/project/fm)
==3905== by 0x401395: name_fm (in /home/suraj/Desktop/project/fm)
==3905== by 0x400C38: main (in /home/suraj/Desktop/project/fm)
==3905== Address 0x51fc724 is 36 bytes inside a block of size 39 alloc'd
==3905== at 0x4C2A2DB: malloc (in /usr/lib/valgrind/vgpreload_memcheck-amd64-linux.so)
==3905== by 0x401064: alloc_matrix (in /home/suraj/Desktop/project/fm)
==3905== by 0x401A59: EliminateXr …
Run Code Online (Sandbox Code Playgroud)