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

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

问题描述

我是 Haskell 的初学者.在此任务中,我正在执行拆分操作,但由于类型不匹配而面临问题.我正在从文本文件中读取数据,并且数据是表格格式.前任.1|2|Rahul|13.25. 采用这种格式.这里 | 是分隔符,所以我想从分隔符 | 中拆分数据并想打印第二列和第四列数据,但我收到这样的错误p>

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'"

这是我的代码..

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

推荐答案

问题是你试图从 main 函数返回一个列表,它的类型是 IO().

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天全站免登陆