实施getReturnList
和getReturnList2
getReturnList
用于计算一次返回a/b
getReturnList2
用于高级版本并行使用分而治之以增加
计算返回a/b
的速度意味着它想要以不同方式并行计算
整个列表的子列表
第一个错误我不明白我将x : []
传递给参数[String]
编译器返回此错误
error for getReturnList
readcsv.hs:108:20:
Couldn't match type `Char' with `[Char]'
Expected type: [String]
Actual type: String
In the return type of a call of `getClose'
In the first argument of `(:)', namely `(getClose (x : []))'
In the expression:
(getClose (x : [])) : getReturnList (n - 1) total xs
readcsv.hs:108:30:
Couldn't match type `[Char]' with `Char'
Expected type: String
Actual type: [String]
In the first argument of `(:)', namely `x'
In the first argument of `getClose', namely `(x : [])'
In the first argument of `(:)', namely `(getClose (x : []))'
代码:
import Control.Monad.Par
import Text.ParserCombinators.Parsec
import Data.Either.Unwrap
import System.IO
{- A CSV file contains 0 or more lines, each of which is terminated
by the end-of-line character (eol). -}
csvFile :: GenParser Char st [[String]]
csvFile =
do result <- many line
eof
return result
-- Each line contains 1 or more cells, separated by a comma
line :: GenParser Char st [String]
line =
do result <- cells
eol -- end of line
return result
-- Build up a list of cells. Try to parse the first cell, then figure out
-- what ends the cell.
cells :: GenParser Char st [String]
cells =
do first <- cellContent
next <- remainingCells
return (first : next)
-- The cell either ends with a comma, indicating that 1 or more cells follow,
-- or it doesn't, indicating that we're at the end of the cells for this line
remainingCells :: GenParser Char st [String]
remainingCells =
(char ',' >> cells) -- Found comma? More cells coming
<|> (return []) -- No comma? Return [], no more cells
-- Each cell contains 0 or more characters, which must not be a comma or
-- EOL
cellContent :: GenParser Char st String
cellContent =
many (noneOf ",\n")
-- The end of line character is \n
eol :: GenParser Char st Char
eol = char '\n'
parseCSV :: String -> Either ParseError [[String]]
--parseCSV :: String -> [[String]]
--parseCSV input = parse csvFile input
parseCSV input = parse csvFile "(unknown)" input
take3 :: Int ->[[String]] -> [String]
take3 0 _ = []
take3 _ [] = []
take3 n (x:xs)
| n == 1 = x
| n > 0 = take3 (n-1) xs
take3 _ _ = error "preludelist.take: negative argument"
returnlimiters = ['\n']
realtake2 :: Int -> [[String]] -> [String]
realtake2 x y = take3 x y
takeelem3 :: Int -> [String] -> String
takeelem3 0 _ = []
takeelem3 _ [] = []
takeelem3 n (x:xs)
| n == 1 = x
| n > 0 = takeelem3 (n-1) xs
takeelem3 _ _ = error "preludelist.take: negative argument"
realtakeelem2 :: Int -> [String] -> String
realtakeelem2 x y = takeelem3 x y
getOpen :: [String] -> String
getOpen x = realtakeelem2 2 x
getHigh :: [String] -> String
getHigh x = realtakeelem2 3 x
getLow :: [String] -> String
getLow x = realtakeelem2 4 x
getClose :: [String] -> String
getClose x = realtakeelem2 5 x
whitespace :: String
whitespace = ['\n','\t',' ']
forloop3 0 f = return()
forloop3 n f =
do
f n
forloop3 (n-1) f
getReturnList :: Int -> Int -> [[String]] -> [[String]]
getReturnList 0 _ _ = []
getReturnList _ _ [] = []
getReturnList n total (x:xs)
| n == total = getReturnList (n-1) total xs
| n > 0 = (getClose (x : [])) : getReturnList (n-1) total xs
getReturnList _ _ _ = error "preludelist.take: negative argument"
getReturnList2 :: Int -> Int -> [[String]] -> [[String]]
getReturnList2 0 _ _ = []
getReturnList2 _ _ [] = []
getReturnList2 n total (x:xs)
| n == total = getReturnList2 (n-1) total xs
| n > 0 =
do
p1 <- spawn (getReturnList2 n-1 total (filter (<x) xs))
p2 <- spawn (getReturnList2 n-1 total (filter (>=x) xs))
left <- get p1
right <- get p2
return $ left ++ (x:right)
getReturnList2 _ _ _ = error "preludelist.take: negative argument"
main = do
csvstring <- readFile "C:\\Users\\martin.lee\\Downloads\\0388.HK.csv"
let afterparse = parseCSV csvstring
let afterparse2 = fromRight afterparse
--putStrLn(show afterparse2)
let getline3 = realtake2 3 afterparse2
putStrLn(show getline3)
--putStrLn(getOpen getline3)
--putStrLn(getHigh getline3)
--putStrLn(getLow getline3)
putStrLn(getClose getline3)
--forloop3 5 (\c -> putStrLn(show (5-c)))
--putStrLn(show (take2b 3 2 3 afterparse2))
--let (as,bs) = splitAt (length grids `div` 2) afterparse2
let num = 5
putStrLn("begin getReturnList")
putStrLn(show ( "a" : []))
putStrLn(show (getReturnList 3 3 afterparse2))
putStrLn("end getReturnList")
--putStrLn(show (1 :: Float))
--putStrLn(show (getlines2 csvstring))
--putStrLn(show (take 1 (getlines2 csvstring)))
putStrLn("a")
更新
F#非并行版
let rec test2(xs : float list) =
if xs.Length > 3 then
let (list1, list2) = split1 (xs.Length/2) xs
let (list3, list4) = split1 (xs.Length/2-1) xs
let b1 = test2(list1) // would like to parallel these two function
let b2 = test2(list4) // would like to parallel these two function
b1 @ b2
else
let b1 = xs |> Seq.windowed 2
|> PSeq.map (fun x -> x.[0]/x.[1])
|> PSeq.toList
b1