nextState :: IO Int -> IO Int -- 0 1 0 2 0 1 0
nextState stateIO = do
value <- stateIO
putStrLn $ "Current state: " ++ show value
fmap (+1) stateIO
nextState' :: IO Int -> IO Int -- 0 1 2
nextState' stateIO = do
value <- stateIO
putStrLn $ "Current state: " ++ show value
return $ value + 1
main :: IO ()
main = do
let startStateIO = return 0 :: IO Int
let …Run Code Online (Sandbox Code Playgroud) template <typename T>
struct Corresponding;
template <>
struct Corresponding<int> {
using CorrespondingT = boost::multiprecison::cpp_int;
};
template <typename T> using GetCorresponding = typename Corresponding<T>::CorrespondingT;
Run Code Online (Sandbox Code Playgroud)
这可以用作
static_assert(std::is_same_v<GetCorresponding<int>, boost::multiprecision::cpp_int>); // true
Run Code Online (Sandbox Code Playgroud)
在编译时解析的Corresponding<T>包含具有相应类型的别名的结构在哪里T。另一个例子是std::remove_ptr_t<T*>对应于T
我可以在 Haskell 中做类似的事情吗,例如
iAmAnInteger :: getCorresponding Int -- Integer
Run Code Online (Sandbox Code Playgroud)
?
我不熟悉 Haskell 的编译时类型功能,但这可能吗?
c++ haskell static-typing compile-time template-specialization
如果我在类或结构中使用typedef或using,有时我希望它独立于用于该类或结构的模板。
在下面的示例中,我会使用Object<T>::RefCountT, 这会起作用,但我宁愿Object::RefCountT在这种情况下使用类似的东西,因为那样我就不必随意选择一种类型(这在阅读时可能会造成混淆)。
template <typename T>
struct Object {
using RefCountT = unsigned short; // This is independent of T
};
Run Code Online (Sandbox Code Playgroud)
对我来说,显而易见(但不理想)的解决方案是在课堂之外定义它,比如
using ObjectRefCountT = unsigned short;
Run Code Online (Sandbox Code Playgroud)
我还尝试在没有模板的情况下进行重新定义,假设它们不会被认为是相同的,但这导致了关于重新定义的预期错误。
我假设因为它是一个类而不是一个函数,所以我不能隐式地这样做,编译器怎么知道它在这里无关紧要?
如果我理解正确的话,下限除法的返回值始终是整数,即使被除数和/或除数不是整数,那么为什么它不总是返回整数。
在我的例子中这是有害的,因为从大浮点数转换为 int 而不是将返回值作为任意精度整数显然会损失精度。
我看不到任何执行浮点除法以返回整数的函数。显然我可以创建一个函数来执行此操作,例如将两个值乘以相同的数量,以便它们都是整数,但它会比 C 实现慢很多。
这是一个例子:5.2 // 2不是2.0。2
python floating-point precision arbitrary-precision floor-division
c++ ×2
haskell ×2
compile-time ×1
optimization ×1
precision ×1
python ×1
recursion ×1
templates ×1
using ×1