是否有带签名的内置功能:: (Monad m) => m a -> a
?
Hoogle告诉我们没有这样的功能.
你能解释一下原因吗?
我试图使用haskell-src-exts包中的parseFile函数解析文件.我正在尝试使用parseFile的输出,这当然是IO,但我无法弄清楚如何绕过IO.我找到了一个函数liftIO,但我不确定这是否是这种情况下的解决方案.这是下面的代码.
import Language.Haskell.Exts.Syntax
import Language.Haskell.Exts
import Data.Map hiding (foldr, map)
import Control.Monad.Trans
increment :: Ord a => a -> Map a Int -> Map a Int
increment a = insertWith (+) a 1
fromName :: Name -> String
fromName (Ident s) = s
fromName (Symbol st) = st
fromQName :: QName -> String
fromQName (Qual _ fn) = fromName fn
fromQName (UnQual n) = fromName n
fromLiteral :: Literal -> String
fromLiteral (Int int) = show int
fromQOp :: QOp …
Run Code Online (Sandbox Code Playgroud) 在阅读 Haskell 教科书中关于不同 monad 的章节时,当作者从解释bind和 monad 定律的细节到实际使用 monad时,我反复迷路。突然,诸如“在 monadic 上下文中运行函数”或“运行 monad”之类的表达出现了。同样,在库文档和关于 monad 转换器堆栈的讨论中,我读到一些函数“可以在任何选择的 monad 中运行”的声明。这个“在 monad 中运行”到底是什么意思?
有两件事我似乎不太明白:
return
, >>=
) 和定律的类型类。因此,在 monad 中“运行”某些东西可能意味着 (a) 将其作为参数提供给return
,或 (b) 使用>>=
. 如果 monad 是 type m a
,那么在 a) 的情况下,某些东西必须是 type a
,以匹配return
函数的类型。如果 b)某事必须是 type 的函数a -> m b
,以匹配函数的类型>>=
。由此,我不明白如何在任意 monad 中“运行”某个函数,因为我排序使用的函数>>=
都必须具有相同的类型签名,并且我使用的值return
必须是特定的 monad 类型参数。run …
我刚读完“学到Haskell来造福伟大!”。书,所以我的问题可能很幼稚。我不明白的是如何从纯代码中调用“不纯”的IO函数。
这是一个用C#编写的工作示例。在我们的业务逻辑中,我们根据天气计划一些行动。我们以通常的C#方式进行操作。
interface IWeatherForecast
{
WeatherData GetWeather(Location location, DateTime timestamp);
}
// concrete implementation reading weather from DB
class DbWeather : IWeatherForecast
{
public override WeatherData GetWeather(Location location, DateTime timestamp)
{...}
}
class WeatherFactory
{
public IWeatherForecast GetWeatherProvider()
{...}
}
// Business logic independent from any DB
class MaritimeRoutePlanner
{
private IWeatherForecast weatherProvider = weatherFactory.GetWeatherProvider();
public bool ShouldAvoidLocation(Location location, DateTime timestamp)
{
WeatherData weather = weatherProvider.GetWeather(location, timestamp);
if(weather.Beaufort > 8)
return true;
else...
...
}
}
Run Code Online (Sandbox Code Playgroud)
如何在Haskell中实现此逻辑?
实际上,“纯逻辑” MaritimeRoutePlanner称之为weatherProvider.GetWeather() …
我理解(有些)monad并理解运算符< - 将从monad中提取值.
但它如何适用于不同类型?
通常,我已经看到它被用于从IO monad中提取字符串.但是在下面的示例代码中,我无法看到它为什么在主要的第3行失败,抱怨它期待一种类型的IO int?编译器如何推断需要IO int?
它(<-
)在multWithLog
方法中做了什么?
import Control.Monad.Trans.Writer.Lazy
main = do
putStrLn $ show $ logNumber 3 -- prints WriterT (Identity (3,["Got Number: 3"]))
putStrLn $ show $ multWithLog -- prints WriterT (Identity (3,["Got Number: 3"]))
_ <- logNumber 3 -- fails with Couldn't match type ‘WriterT [String] Data.Functor.Identity.Identity’ with ‘IO’
-- Expected type: IO Int
-- Actual type: Writer [String] Int
putStrLn "test"
logNumber :: Int -> Writer [String] Int
logNumber x …
Run Code Online (Sandbox Code Playgroud) 我还是Haskell的初学者,所以看了writefile
网上的一些教程,看到writefile
网上的例子大部分都是在main
函数里面使用的(main = IO())
我想知道是否可以编写一个函数,在计算结果writefile
时使用它将结果写入文件?在某些程序(尤其是游戏)中,用户可能希望通过将内容保存到 .txt 文件来在游戏的特定点停止。
例如这样的:(这个功能不起作用,只是想知道如何使它起作用)
concat :: FilePath -> [[a]] -> [a]
concat txt [] = []`
concat txt (xs : xss) = do
y <- xs ++ concat xss
writeFile txt (unlines y)
Run Code Online (Sandbox Code Playgroud)
谢谢:)