string -++ 运算符与字符串和 IO 字符串

标签 string haskell io

explandDol :: String -> String -> [String] -> IO String
explandDol conclusion operators atoms =
    let (ys,zs) = splitAt (head (take 1 replacement)) conclusion in ys ++ getConclusion     operators atoms ++ (tail zs)
    where replacement = elemIndices '$' conclusion



getConclusion :: String -> [String] -> IO String
getConclusion operators atoms =
   runRVar (choice [atom1 ++ " " ++ [operator] ++ " " ++ atom2 | atom1 <- atoms, atom2 <-    atoms, operator <- operators,checkAtoms atom1 atom2]) StdRandom

有什么好的办法可以解决这个问题吗?我收到此错误:

/home/joe/Documents/haskell/LAG/main/main.hs: line 73, column 69:
  Couldn't match expected type `IO String' with actual type `[Char]'
  In the expression: ys ++ getConclusion operators atoms ++ (tail zs)
  In the expression:
    let (ys, zs) = splitAt (head (take 1 replacement)) conclusion
    in ys ++ getConclusion operators atoms ++ (tail zs)
  In an equation for `explandDol':
      explandDol conclusion operators atoms
        = let (ys, zs) = splitAt (head (take 1 replacement)) conclusion
          in ys ++ getConclusion operators atoms ++ (tail zs)
        where
            replacement = elemIndices '$' conclusion
/home/joe/Documents/haskell/LAG/main/main.hs: line 73, column 75:
  Couldn't match expected type `[Char]' with actual type `IO String'
  In the return type of a call of `getConclusion'
  In the first argument of `(++)', namely
    `getConclusion operators atoms'
  In the second argument of `(++)', namely
    `getConclusion operators atoms ++ (tail zs)'
/home/joe/Documents/haskell/LAG/main/main.hs: line 73, column 75:
  Warning: Redundant bracket
  Found:
    getConclusion operators atoms ++ (tail zs)
  Why not:
    getConclusion operators atoms ++ tail zs

最佳答案

因为 getConclusion 返回的值是 IO String您不能简单地将它与对未包装值进行操作的函数一起使用。首先使用 x <- getConclusion operators atom 解包值或者如果你想要函数组合使用 fmap .

关于string -++ 运算符与字符串和 IO 字符串,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/16051951/

相关文章:

java - 如何计算序列在 Java 字符串中出现的次数?

java - 使用 StringTokenizer 和连接字符串之间有什么区别吗?

java - 确保单词具有特定长度

haskell - 功能性镜片

haskell - 如何捕获在 C 函数调用的 Haskell 回调函数中引发的 Haskell 异常?

linux - 输入重定向到 echo

c - 无法在 C 中正确读取文件

c++ - 改变参数值和数量——这是合法的 C++ 吗?

haskell - Haskell 中的 Map.toList 性能

c++ - 遍历内存以读取值