我如何在GHCi中运行多个语句?

问题描述 投票:-1回答:3

我正在对一个简单的函数进行一些非常简单的性能测试,我认为它具有O(n)(平方)个性能(或更差)。

当前,我正在运行多个要重复的语句:

ghci> myfunction 0 100
true
ghci> myfunciton 0 200
true
ghci> myfunction 0 300
true
ghci> :r

有没有一种方法可以运行所有四个GHCi语句?我不能只使用“本机” Haskell来组合它们,因为我想包含最后运行的:r(这是GHCi语句-并非完全是Haskell)。

haskell testing multiline ghci
3个回答
2
投票

您可以通过以下方式使用:def定义自定义GHCi命令:

> :def foo (\_ -> return "print 100\nprint 200\n:t length")
> :foo
100
200
length :: Foldable t => t a -> Int

在返回的字符串中,也可以包含:-命令,就像上面的:t


3
投票

我发现的一种方法是创建一个单独的文件:

myfunction 0 100
myfunction 0 200
myfunction 0 300
:r

然后使用:

:script path/to/file


2
投票

一种方法是在Cabal文件中创建一个测试套件,在其中将函数调用作为测试,然后使用stack test --file-watch。每次保存文件时,都会重新编译并重新运行测试。

© www.soinside.com 2019 - 2024. All rights reserved.