我有一个Haskell程序,它将文件作为输入并将其转换为二进制搜索树。Haskell IO:将IO字符串转换为“其他类型”
import System.IO
data Tree a = EmptyBST | Node a (Tree a) (Tree a) deriving (Show, Read, Eq)
ins :: Ord a => a -> (Tree a) -> (Tree a)
ins a EmptyBST = Node a EmptyBST EmptyBST
ins a (Node p left right)
| a < p = Node p (ins a left) right
| a > p = Node p left (ins a right)
| otherwise = Node p left right
lstToTree :: Ord a => [a] -> (Tree a)
lstToTree = foldr ins EmptyBST
fileRead = do file <- readFile "tree.txt"
let a = lstToTree (conv (words file))
return a
conv :: [String] -> [Int]
conv = map read
然而,当我运行以下命令:
ins 5 fileRead
我得到了以下错误:
<interactive>:2:7:
Couldn't match expected type `Tree a0'
with actual type `IO (Tree Int)'
In the second argument of `ins', namely `fileRead'
In the expression: ins 5 fileRead
In an equation for `it': it = ins 5 fileRead
请没有人能帮助我吗?
感谢
谢谢,但我希望用户输入他想添加到列表中的任何元素。我不希望元素被修复 – modafarhan 2013-03-26 17:00:24
然后修改上面的内容:通过'getLine'读取元素并使用它,添加某种循环来添加从标准输入读取的许多元素,无论您喜欢什么。 – us2012 2013-03-26 17:02:59