Привет я пытаюсь выучить Haskell и сравнить его производительность на других языках , когда я запускаю следующий код ..сроки эксперимента по BST в 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();
при попытке запустить этот код я получить
Prelude>: нагрузка "bst.hs" [1 из 1] Compiling BST (bst.hs, интерпретируемые)
bst.hs: 83: 29: ошибка синтаксического анализа на входе `< -» Неудачные, модули загружены : никто.
Я исчерпал свои знания о haskell. Я попытался переместить операторы модуля как до, так и после включений, но ни одна из них не помогла. Я использовал как bst, так и временной код отдельно, но в сочетании вызывает ошибку
random_numbers.txt - это список значений, разделенных запятой.
Вы можете обнаружить, что двойная обратная и зачистка каждой подстроки медленнее, чем запуск строки через 'filter (not.isSpace)' first. – AndrewC