无法将类型`[]'与`IO' - Haskell匹配 [英] Couldn't match type `[]' with `IO' -- Haskell

查看:198
本文介绍了无法将类型`[]'与`IO' - Haskell匹配的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我是Haskell的初学者。在这个任务我执行拆分操作,但我面临的问题,因为类型不匹配。我从文本文件读取数据,数据是表格式。例如, 1 | 2 | Rahul | 13.25。在此格式中。这里 | 是分隔符,因此我想从分隔符 | 拆分数据,并打印第2列和第4列数据但我得到这样的错误

 无法与`IO'匹配类型`[]'
预期类型:IO [Char]
实际类型:[[Char]]
在splitOn的调用的返回类型中
pre>

这是我的代码..

  b 
$ b import Data.List.Split

main = do
list< - readFile(src / table.txt)
putStrLn list
splitOn| list

任何有关这方面的帮助都会感谢。感谢

解决方案

问题是你试图从 main 函数返回一个列表, code> IO()。



您可能想要打印的结果。

  main = do 
list< - readFile(src / table.txt)
putStrLn list
print $ splitOn | list


I'm beginner in Haskell. In this task i'm performing the split operation but i'm facing problem because of type mis match. I'm reading data from text file and the data is in table format. Ex. 1|2|Rahul|13.25. In this format. Here | is delimiter so i want to split the data from the delimiter | and want to print 2nd column and 4th column data but i'm getting the error like this

  "Couldn't match type `[]' with `IO'
    Expected type: IO [Char]
      Actual type: [[Char]]
    In the return type of a call of `splitOn'"

Here is my code..

module Main where

import Data.List.Split

main = do
    list <- readFile("src/table.txt")
    putStrLn list
    splitOn "|" list

Any help regarding this will appreciate.. Thanks

解决方案

The problem is that you're trying to return a list from the main function, which has a type of IO ().

What you probably want to do is print the result.

main = do
    list <- readFile("src/table.txt")
    putStrLn list
    print $ splitOn "|" list

这篇关于无法将类型`[]'与`IO' - Haskell匹配的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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