简体   繁体   中英

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 . 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.

import Control.Monad (filterM)
powerset = filterM (const [True, False])

main =  powerset[1,2]

the problem is 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

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM