小编Lam*_*ist的帖子

如何在Haskell中导入.hs文件

我做了一个名为的文件time.hs.它包含一个函数,用于测量另一个函数的执行时间.

有没有办法将time.hs文件导入另一个Haskell脚本?

我想要的东西:

module Main where
import C:\Haskell\time.hs

main = do
    putStrLn "Starting..."
    time $ print answer
    putStrLn "Done."
Run Code Online (Sandbox Code Playgroud)

时间在'time.hs'中定义为:

module time where
Import <necessary modules>

time a = do
start <- getCPUTime
v <- a
end   <- getCPUTime
let diff = (fromIntegral (end - start)) / (10^12)
printf "Computation time: %0.3f sec\n" (diff :: Double)
return v
Run Code Online (Sandbox Code Playgroud)

我不知道如何导入或加载单独的.hs文件.time.hs在导入之前是否需要将文件编译到模块中?

syntax haskell module

57
推荐指数
2
解决办法
3万
查看次数

创建一个允许类型在每次重复函数调用后更改的折叠,以便在没有递归的情况下调用函数n次

我试图使用定义的dfold 这里

dfold 
    :: KnownNat k    
    => Proxy (p :: TyFun Nat * -> *)    
    -> (forall l. SNat l -> a -> (p @@ l) -> p @@ (l + 1))  
    -> (p @@ 0) 
    -> Vec k a  
    -> p @@ k
Run Code Online (Sandbox Code Playgroud)

基本上它是一个折叠,允许您在每个循环后返回一个新类型.

我试图概括这个项目中定义的bitonicSort:https: //github.com/adamwalker/clash-utils/blob/master/src/Clash/Sort.hs

我有两个函数对dfold生成的类型很重要:

bitonicSort
    :: forall n a. (KnownNat n, Ord a)
    => (Vec n a -> Vec n a)             -- ^ The recursive step
    -> (Vec (2 * n) a …
Run Code Online (Sandbox Code Playgroud)

haskell type-systems clash type-level-computation

19
推荐指数
1
解决办法
488
查看次数

在Haskell中实现函数重载

我正在研究编写类似于C++程序的Haskell代码的问题.

C++代码是:

class Rectangle
{
    private:
        int length;
        int width;
    public:
        Rectangle()
        {
            length = 0;
            width = 0;
        }
        Rectangle(int x)
        {
            length = x;
            width =0;
        }
        Rectangle ( int x , int y)
        {
            length = x;
            width = y;
        }
};
Run Code Online (Sandbox Code Playgroud)

为了编写类似的Haskell代码,我创建了一个数据类型Rectangle

data Rectangle = Rectangle Length Width deriving (Eq, Show , Read)
type Length = Int
type Width = Int
Run Code Online (Sandbox Code Playgroud)

然后我想到了一个可以作为构造函数的加载函数.但我不明白如何使用不同数量的参数实现函数重载.请帮忙.谢谢.

haskell

6
推荐指数
2
解决办法
3640
查看次数

如何实现类似std :: copy_if之类的东西,但在插入另一个容器之前应用一个函数

完全公开,这可能是在没有需要时尝试使用STL算法的锤子和钉子情况.我在一些C++ 14代码中看到了一个重新出现的模式.我们有一个迭代的容器,如果当前元素匹配某些条件,那么我们将其中一个元素字段复制到另一个容器.

模式类似于:

 for (auto it = std::begin(foo); it!=std::end(foo); ++it){
    auto x = it->Some_member;
    // Note, the check usually uses the field would add to the new container. 
    if(f(x) && g(x)){ 
      bar.emplace_back(x);
    }
  }
Run Code Online (Sandbox Code Playgroud)

这个想法几乎是一个累积,其中所应用的函数并不总是返回一个值.我只能想到一个解决方案

这甚至是个好主意吗?

c++ stl c++14

6
推荐指数
2
解决办法
270
查看次数

为什么嵌套在其他monad中的IO不能执行?有没有办法迫使他们去?

这是我上一个问题的后续跟进. IO动作嵌套在未执行的其他monad中

该问题的解决方案是删除一些monad,并允许执行IO操作.

为什么我需要取消monad?有没有办法在没有取消的情况下执行IO?

注意:这是一个假设,而不是关于好的或坏的做法的问题.

monads haskell

4
推荐指数
2
解决办法
298
查看次数