English 中文(简体)
Snippet(闭门)
原标题:Error in the code Snippet [closed]
  • 时间:2012-01-16 10:06:36
  •  标签:
  • haskell

有些人可以解释以下几页的代码错误。 这部法律试图在接收用户的意见时执行《商业仲裁示范法》的序幕。

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

错误一是:

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 [];
       .... }
问题回答

Some problems I see with the code are:

  1. You re calling a loop function that you did not define or import anywhere
  2. You did not indent the contents of main
  3. You write ys <- inorder numtree, which implies that inorder returns an IO something, but it does not. You should use let instead.
  4. Your do-block ends with a <- assignemnt - it should end with an expression.




相关问题
Euler Problem in Haskell -- Can Someone Spot My Error

I m trying my hand at Euler Problem 4 in Haskell. It asks for that largest palindrome formed by multiplying two three-digit numbers. The problem was simple enough, and I thought my Haskell-fu was up ...

How does foldr work?

Can anybody explain how does foldr work? Take these examples: Prelude> foldr (-) 54 [10, 11] 53 Prelude> foldr (x y -> (x+y)/2) 54 [12, 4, 10, 6] 12.0 I am confused about these executions....

Efficient queue in Haskell

How can I efficiently implement a list data structure where I can have 2 views to the head and end of the list, that always point to a head a tail of a list without expensive calls to reverse. i.e: ...

Problem detecting cyclic numbers in Haskell

I am doing problem 61 at project Euler and came up with the following code (to test the case they give): p3 n = n*(n+1) `div` 2 p4 n = n*n p5 n = n*(3*n -1) `div` 2 p6 n = n*(2*n -1) p7 n = n*(5*n -3)...

Ways to get the middle of a list in Haskell?

I ve just started learning about Functional Programming, using Haskel. I m slowly getting through Erik Meijer s lectures on Channel 9 (I ve watched the first 4 so far) and in the 4th video Erik ...

haskell grouping problem

group :: Ord a => [(a, [b])] -> [(a, [b])] I want to look up all pairs that have the same fst, and merge them, by appending all the list of bs together where they have the same a and discarding ...

Closest equivalent to subprocess.communicate in Haskell

I want to do a popen() / python s subprocess.communicate from Haskell - start a program, give it stdin, and get its stdout/stderr. What s the most direct / Haskellish way to do this?

热门标签