Haskell 代码段中出现错误
有人能解释一下下面代码片段中的错误吗。在这段代码中,我尝试在获取用户输入的同时实现BST的顺序遍历Haskell 代码段中出现错误,haskell,Haskell,有人能解释一下下面代码片段中的错误吗。在这段代码中,我尝试在获取用户输入的同时实现BST的顺序遍历 import System.IO loop :: Int -> [Int] -> IO [Int] loop 0 ls = return ls loop n ls = do newNumber <- readLn loop (n-1) (newNumber:ls) data Tree a = EmptyTree | Node a ( Tree a
import System.IO
loop :: Int -> [Int] -> IO [Int]
loop 0 ls = return ls
loop n ls = do newNumber <- readLn
loop (n-1) (newNumber:ls)
data Tree a = EmptyTree | Node a ( Tree a) ( Tree a) deriving ( Show,Read, Eq)
singleton :: a -> Tree a
singleton x = Node x EmptyTree EmptyTree
treeInsert :: ( Ord a) => a -> Tree a -> Tree a
treeInsert x EmptyTree = singleton x
treeInsert x ( Node a left right)
| x==a = Node x left right
| x<a = Node a (treeInsert x left) right
| x>a = Node a left (treeInsert x right)
inorder :: Tree a -> [a]
inorder EmptyTree = []
inorder (Node a left right) = inorder left ++ [a] ++ inorder right
main = do
putStrLn " Please enter the number"
number <- readLn :: IO Int
putStrLn $ "The num is:" ++ show number
xs <- loop number []
let numtree = foldr treeInsert EmptyTree xs
print numtree
ys <- inorder numtree
print ys
import System.IO
循环::Int->[Int]->IO[Int]
循环0 ls=返回ls
loop n ls=do newNumber树a
单例x=节点x空树空树
树插入::(Ord a)=>a->a树->a树
treeInsert x EmptyTree=单例x
树插入x(节点a左-右)
|x==a=节点x左右
|xa=节点a左(树插入x右)
索引::树a->[a]
顺序空树=[]
顺序(节点a左-右)=顺序左+++[a]++顺序右
main=do
putStrLn“请输入号码”
数字我看到代码中的一些问题是:
您正在调用未在任何位置定义或导入的循环
函数
您没有缩进main
编写ys如果编译器给你一个错误,请将错误消息添加到问题中。如果没有,请解释您期望代码做什么以及它实际做什么。请不要反复问(几乎)相同的问题。你期待什么?因此,不是由人类驱动的分布式编译器。有人可以结束这个问题吗?@user1145428:如果代码不完整,而你甚至不告诉我们出了什么问题,你希望我们如何回答?好的,对不起大家。。下次我会把整个问题清楚地贴出来
Couldn't match expected type `IO t0' with actual type `[a0]'
In the return type of a call of `inorder'
In a stmt of a 'do' expression: ys <- inorder numtree
In the expression:
do { putStrLn " Please enter the number";
number <- readLn :: IO Int;
putStrLn $ "The num is:" ++ show number;
xs <- loop number [];
.... }