在哈斯克尔的bst定时实验

时间:2015-02-22 22:41:25

标签: haskell binary-search-tree timing

您好我正在尝试学习haskell并将其性能与其他语言进行比较 当我运行以下代码..

module BST (
  Tree,
  singletonTree,
  insert,
  member
) where


import System.IO
import System.IO.Error hiding (try)
import Control.Exception
import Data.Char
import System.CPUTime


--
-- Take the string and convert it to a list of numbers
--
trim = f . f
   where f = reverse . dropWhile isSpace
fromDigits = foldl addDigit 0
       where addDigit num d = 10*num+d
strToInt str = fromDigits (map digitToInt str)
split_comma "" = []
split_comma input = 
        let (a,b) = break (\x->x==',') input in 
        [(trim a)]++(split_comma (drop 1 b))
make_int_list input =map strToInt (split_comma input)
-- end of converting string to integers




data Tree a = EmptyTree | Node a (Tree a)(Tree a) deriving (Show)

singletonTree :: a -> Tree a
singletonTree x = Node x EmptyTree EmptyTree

insert :: Ord a => a -> Tree a -> Tree a
insert x EmptyTree = singletonTree x
insert x (Node root left right) 
    | x < root = Node root (insert x left) (right)
    | x > root  = Node root (left) (insert x right)
    | x == root = Node root (Node x left EmptyTree) (right) 

member :: Ord a => a -> Tree a -> Bool
member x EmptyTree = False
member x (Node n left right)
  | x == n = True
  | x < n = member x left
  | x > n = member x right


---A test function to do the timing
test_func input_list =do
      startTime <- getCPUTime
      --Note: If you don't use any results haskell won't even run the code
      -- if you just mergesrt here (uncomment next line) instead of print
      -- return (let tree = foldr insert EmptyTree )
      -- then it will always take 0 seconds since it won't actually sort!
      let tree = foldr insert EmptyTree input_list
      prin(tree)
      finishTime <- getCPUTime
      return $ fromIntegral (finishTime - startTime) / 1000000000000

main :: IO ()
main = do 
       inh <- openFile "random_numbers.txt" ReadMode
       mainloop inh 
       hClose inh
--Read in my file and run test_func on input
mainloop :: Handle -> IO ()
mainloop inh  = 
    do input <- try (hGetLine inh)
       case input of
         Left e -> 
             if isEOFError e
                then return ()
                else ioError e
         Right inpStr ->
             do
        let my_list = make_int_list inpStr;
            my_time <- test_func my_list
                    putStrLn ("Execution time in Sections: ")
                         print(my_time);
                    return ();

尝试运行此代码时,我得到了

前奏&GT; :load&#34; bst.hs&#34; [1 of 1]编译BST(bst.hs,解释)

bst.hs:83:29:解析输入`&lt; - &#39; 失败,模块加载:无。

我已经用尽了我对haskell的了解。我尝试将模块语句移动到包含之前和之后,但都没有帮助。我分别使用了bst和时间码,但是组合导致了错误

random_numbers.txt是逗号分隔值的列表。

1 个答案:

答案 0 :(得分:1)

最后一个do块格式不正确。这是一个差异:

@@ -78,9 +78,7 @@
                 then return ()
                 else ioError e
          Right inpStr ->
-             do
-        let my_list = make_int_list inpStr;
-            my_time <- test_func my_list
-                    putStrLn ("Execution time in Sections: ")
-                         print(my_time);
-                    return ();
+             do let my_list = make_int_list inpStr;
+                my_time <- test_func my_list
+                putStrLn("Execution time in Sections: ")
+                print(my_time)

注意:

  • 我没有在源中的任何地方使用制表符;我有一种感觉,你的来源使用标签。我的建议是避免使用Haskell源代码中的标签。
  • 您不需要parens来调用函数 - putStrLn "..."print my_time将起作用

此外,prin(tree)之前应为print(tree),但更常见的是print tree - 不需要填充。