Haskell powerset函数 - 如何避免与实际类型`[[Integer]]'不匹配预期类型`IO t0'' [英] Haskell powerset function - How to avoid Couldn't match expected type `IO t0' with actual type `[[Integer]]'

查看:338
本文介绍了Haskell powerset函数 - 如何避免与实际类型`[[Integer]]'不匹配预期类型`IO t0''的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个powerset实现,我试图在这里运行: http://rextester.com/runco​​de。我仍然得到这个错误,并不知道如何使其正确。我试图尽可能多地阅读haskell中的IO,但这对我来说太难了。

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

main = powerset [1,2]
main = ...

$ b

解决方案


$ b

main 应该有类型 IO() code> [[Integer]] (正如编译器告诉你的) - 所以我认为你想把结果输出到控制台,我想你正在寻找
import Control.Monad(filterM)
$ b $ powerset = filterM(const [True,False])
$ b $ main =
print $ powerset [1,2]

有人说过,您应该添加顶级签名:

 模块Main其中

导入Control.Monad(filterM)

powerset :: [a] - > [[a]]
powerset = filterM(const [True,False])

main :: IO()
main =
print $(powerset [1 ,2] :: [[Int]])



来自评论的其他问题



stdin 获取输入的常用方式是 getLine :: IO String


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

这篇关于Haskell powerset函数 - 如何避免与实际类型`[[Integer]]'不匹配预期类型`IO t0''的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆