[英]Haskell powerset function - How to avoid Couldn't match expected type `IO t0' with actual type `[[Integer]]'
I have a powerset implementation that I'm trying to run here: http://rextester.com/runcode . 我有一个要在此处运行的Powerset实现: http ://rextester.com/runcode。 I'm still getting this error and can't figure out how to make it right.
我仍然遇到此错误,无法找出正确的方法。 I'm trying to read as much as possible about IO in haskell but it is super hard for me.
我正在尝试尽可能多地阅读有关Haskell中的IO的信息,但这对我来说真的很难。
import Control.Monad (filterM)
powerset = filterM (const [True, False])
main = powerset[1,2]
the problem is main = ...
问题是
main = ...
main
should have type IO ()
but you give an expression with type [[Integer]]
(as the compiler tells you) - so as I think you want to output the result to the console I think you are looking for print
main
应该具有IO ()
类型,但是您要给出一个[[Integer]]
类型的表达式(正如编译器告诉您的)-因此,我认为您想将结果输出到控制台,我认为您正在寻找print
this works for me: 这对我有用:
import Control.Monad (filterM)
powerset = filterM (const [True, False])
main =
print $ powerset[1,2]
having said this you should add the top-level signatures: 话虽如此,您应该添加顶级签名:
module Main where
import Control.Monad (filterM)
powerset :: [a] -> [[a]]
powerset = filterM (const [True, False])
main :: IO ()
main =
print $ (powerset [1,2] :: [[Int]])
the usual way to get input from stdin is getLine :: IO String
从stdin获取输入的通常方法是
getLine :: IO String
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.