简体   繁体   English

Haskell powerset函数-如何避免无法将期望的类型“ IO t0”与实际类型“ [[Integer]]”匹配

[英]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/runco​​de。 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]])

additional question from comment 评论中的其他问题

the usual way to get input from stdin is getLine :: IO String stdin获取输入的通常方法是getLine :: IO String

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 Haskell:无法将期望的类型“ IO t0”与实际类型“ Integer”匹配 - Haskell: Couldn't match expected type 'IO t0' with actual type 'Integer' Haskell:无法将预期类型“IO t0”与实际类型“[Char]”匹配 - Haskell: Couldn't match expected type ‘IO t0’ with actual type ‘[Char]’ 无法匹配预期类型`[([Char],a0)]'与实际类型`([Char],t0)'Haskell - Couldn't match expected type `[([Char], a0)]' with actual type `([Char], t0)' Haskell Haskell无法将期望的类型“ Integer”与实际类型“ [Integer]”进行匹配 - Haskell Couldn't match expected type `Integer' with actual type `[Integer]' Haskell无法将预期类型[char]与实际类型IO匹配 - Haskell couldn't match expected type [char] with actual type IO 无法将预期类型“(t1,t2,t0)”与实际类型“ [a0]”匹配 - Couldn't match expected type `(t1, t2, t0)' with actual type `[a0]` 无法将预期类型(Int - > Int - > Int)与实际类型`(t0,t1,t2)'匹配 - couldn't match expected type (Int -> Int -> Int) with actual type `(t0, t1, t2)' “无法将类型'Maybe'与'IO'匹配预期类型:IO String实际类型:Maybe String”在Haskell中 - “Couldn't match type `Maybe' with `IO' Expected type: IO String Actual type: Maybe String” In Haskell 无法将预期类型`Maybe(String,Int,String)'与实际类型`([Char],t0,[Char])'匹配 - Couldn't match expected type `Maybe (String, Int, String)' with actual type `([Char], t0, [Char])' 无法将预期类型'(Int,Int)'与实际类型'[t0]'相匹配 - Couldn't match expected type ‘(Int, Int)’ with actual type ‘[t0]’
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM