我是否违反了One Definition Rule并使用以下程序?
// foo.hpp
#ifndef FOO_HPP_
#define FOO_HPP_
namespace {
inline int foo() {
return 1;
}
}
inline int bar() {
return foo();
}
#endif
//EOF
Run Code Online (Sandbox Code Playgroud)
和
// m1.cpp
#include "foo.hpp"
int m1() {
return bar();
}
//EOF
Run Code Online (Sandbox Code Playgroud)
和
// m2.cpp
#include "foo.hpp"
int m2() {
return bar();
}
//EOF
Run Code Online (Sandbox Code Playgroud)
最后
// main.cpp
#include <iostream>
int m1();
int m2();
int main(int, const char* [])
{
int i = m1();
int j = m2();
std::cout << (i+j) << std::endl; …
Run Code Online (Sandbox Code Playgroud) 所以我们有:
import Control.Monad.Writer.Strict
type M a = Writer (Map Key Val) a
Run Code Online (Sandbox Code Playgroud)
一些Key
和Val
.
只要我们不查看收集的输出,一切都可以正常工作:
report comp = do
let (a,w) = runWriter comp
putStrLn a
Run Code Online (Sandbox Code Playgroud)
但是,如果我们想要检查w
,我们会得到堆栈溢出.
report comp = do
let (a,w) = runWriter comp
guard (not $ null w) $ do -- forcing w causes a stack overflow
reportOutputs w
putStrLn a
Run Code Online (Sandbox Code Playgroud)
我认为原因是因为(>>=)
for Writer
被定义为:
m >>= k = WriterT $ do
(a, w) <- runWriterT m
(b, w') <- …
Run Code Online (Sandbox Code Playgroud) 所以我想说我在Haskell中编写了一些类型级程序:
type family NAryFn (n::Nat) (dom::*) (cod::*) :: *
type instance NAryFn Ze dom cod = cod
type instance NAryFn (Su n) dom cod = dom -> NAryFn n dom cod
Run Code Online (Sandbox Code Playgroud)
我认为这很有用,我想在我的项目中使用它.所以我把它放在一个模块中.
什么是模块的良好分层名称?(参见Haskell Hierarchical Modules)
很多数据结构的住在Data
(Data.Text
,Data.List
,等),结构化影响各种方式在Control
如Control.Monad
或Control.Applicative
.
类型级程序应该在哪里生活? Type
?TypeFamily
?已经达成共识了吗?
我目前安装了Xcode 4.6.3.今天App Store告诉我Xcode 5可用.
我应该升级,还是升级会破坏GHC 7.6.3?
我在看ghc trac#8197和ghc trac#8148,他们似乎暗示会有问题.那是对的吗?
我没有直接与NUnit合作,但我希望在不同的背景下借鉴它的一些想法.
一个特别优雅的想法是约束机制,它允许您编写表单的单元测试:
Assert.That(aValue, Is.GreaterThan(2.0) & Is.LessThan(5.0));
Run Code Online (Sandbox Code Playgroud)
您还可以测试某个值在某个范围内:
Assert.That(aValue, Is.InRange(2.0, 5.0));
Run Code Online (Sandbox Code Playgroud)
但是,似乎没有办法测试它aValue
是允许值的集合之一:
Assert.That(aValue, Is.OneOf(aCollection));
Run Code Online (Sandbox Code Playgroud)
在单元测试中它不常见吗?它是否表明我的单元测试存在一些问题?每个人都只是将aValue注入一个虚拟的一个元素集合然后使用Is.SubsetOf
吗?
input <- readLn
if (input == 0)
then
putStr "0"
else if (input ==1)
then
putStr "1"
else if (input ==2)
Run Code Online (Sandbox Code Playgroud)
在这种情况下如何使用多个putStr在一个then
或else if
?
当我尝试收到错误
Type error in application
*** Expression : putStr "0" putStr "0"
*** Term : putStr
*** Type : String -> IO ()
*** Does not match : a -> b -> c -> d
Run Code Online (Sandbox Code Playgroud) 我正在使用parsec编写源到源的转换,所以我有一个LanguageDef
for my language,我TokenParser
使用它构建一个for Text.Parsec.Token.makeTokenParser
:
myLanguage = LanguageDef { ...
commentStart = "/*"
, commentEnd = "*/"
...
}
-- defines 'stringLiteral', 'identifier', etc...
TokenParser {..} = makeTokenParser myLanguage
Run Code Online (Sandbox Code Playgroud)
不幸的是,因为我定义了,commentStart
并且commentEnd
,每个解析器组合器TokenParser
都是一个以词语形式实现的词法解析器whiteSpace
,并且whiteSpace
吃空格和注释.
在这种情况下保留评论的正确方法是什么?
我能想到的方法:
commentStart
和commentEnd
.将每个lexeme解析器包装在另一个组合器中,该组合器在解析每个标记之前抓取注释.makeTokenParser
(或者使用一些概括的库Text.Parsec.Token
;如果是,哪个库?)在这种情况下做了什么?
我有一些代码需要很长时间才能运行(几个小时),我认为这是因为它在if语句中进行了大量的比较.我希望它运行得更快,有没有人有任何有用的建议来改善运行时?如果有人对减慢代码的速度有不同的看法,那么我可以尝试修复它,我们将不胜感激.
xPI = zeros(1,1783);
argList2 = zeros(1,1783);
aspList2 = zeros(1,1783);
cysList2 = zeros(1,1783);
gluList2 = zeros(1,1783);
hisList2 = zeros(1,1783);
lysList2 = zeros(1,1783);
tyrList2 = zeros(1,1783);
minList= xlsread('20110627.xls','CM19:CM25');
maxList= xlsread('20110627.xls','CN19:CN25');
N = length(pIList);
for i = 1:N
if (argList(i)>= minList(1) && argList(i) <= maxList(1)) ...
&& (aspList(i)>= minList(2) && aspList(i) <= maxList(2)) ...
&& (cysList(i)>= minList(3) && cysList(i) <= maxList(3)) ...
&& (gluList(i)>= minList(4) && gluList(i) <= maxList(4)) ...
&& (hisList(i)>= minList(5) && hisList(i) <= maxList(5)) ...
&& (lysList(i)>= minList(6) && …
Run Code Online (Sandbox Code Playgroud) 在下面的代码中,我试图创建一个函数"patient_count",它是"horse","pig"和"dog"类的朋友.我可以让这个功能成为一个班级的朋友而不是所有的3个人.任何人都能告诉我我的错误是什么吗?
/*******************************************************\
* Veternarian Class Problem - I need a class for each *
* of 3 animals. Horse, Pig and Dogs *
\*******************************************************/
#include <cstdlib>
#include <iostream>
#include <string>
const int HORSE_KENNEL = 100; // Number of horses we can store
const int PIG_KENNEL = 100; // Number of Pigs we can store
const int DOG_KENNEL = 100; // Number of Dogs we can store
/*******************************************************\
* Class horse *
* *
* Member functions *
* horse_count -- …
Run Code Online (Sandbox Code Playgroud) haskell ×5
c++ ×2
collections ×1
comments ×1
data-kinds ×1
do-notation ×1
dsl ×1
ghc ×1
if-statement ×1
macos ×1
matlab ×1
module ×1
monads ×1
namespaces ×1
nunit ×1
parsec ×1
parsing ×1
profiling ×1
runtime ×1
strictness ×1
unit-testing ×1
xcode5 ×1