相关疑难解决方法(0)

Haskell IO(String)和String

我想写函数并将结果放到字符串中.

我想要的功能:

read' :: FilePath -> String
Run Code Online (Sandbox Code Playgroud)

我用:

:t readFile
readFile :: FilePath -> IO String
Run Code Online (Sandbox Code Playgroud)

我做:

read' :: IO ()
read' = do
     str <- readFile "/home/shk/workspace/src/test.txt" 
     putStrLn str
Run Code Online (Sandbox Code Playgroud)

我想问str是不是字符串?

我们知道:

:t putStrLn
putStrLn :: String -> IO ()
Run Code Online (Sandbox Code Playgroud)

那我为什么不能:

read' :: String
read' = do
     str <- readFile "/home/shk/workspace/lxmpp/src/test.txt" 
     str
Run Code Online (Sandbox Code Playgroud)

我得到错误:

 Couldn't match expected type `[t0]' with actual type `IO String'
    In the return type of a call of `readFile'
    In a stmt of a 'do' expression:
        str …
Run Code Online (Sandbox Code Playgroud)

io haskell input

7
推荐指数
2
解决办法
2749
查看次数

Haskell奇怪的回归

checkstring :: [String] -> Int -> [String]
checkstring p n = do    z <- doesFileExist (p !! n)
    if z
    then p
    else error $ "'" ++ (p !! n) ++ "' file path does not exist"
Run Code Online (Sandbox Code Playgroud)

它通过查看"n"检查字符串中的元素(因此,如果n = 2,它将检查列表中的第二个字符串),然后查看它是否存在.如果它确实存在,它将返回原始字符串列表,如果不存在则会出错.为什么这样做?:

Couldn't match expected type `[t0]' with actual type `IO Bool'
    In the return type of a call of `doesFileExist'
    In a stmt of a 'do' expression: z <- doesFileExist (p !! n)
Run Code Online (Sandbox Code Playgroud)

haskell haskell-platform

3
推荐指数
1
解决办法
325
查看次数

如何评估Haskell中的IO Bools

我正在尝试编写一个带有IO Bool的函数,并根据这是什么做的,但我无法弄清楚如何评估IO Bool.我试着说do cond,do {cond==True}但得到了错误Couldn't match expected type 'Bool' against inferred type 'a b'.有人可以指导我找到解决方案吗?

monads haskell

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

标签 统计

haskell ×3

haskell-platform ×1

input ×1

io ×1

monads ×1