我有这段代码:
main :: IO ()
main = do
let list = []
handle <- openFile "polycake.in" ReadMode
contents <- hGetContents handle
let singlewords = words contents
list = fileToList singlewords
n = head list
list' = drop 1 list
polygons = polyList n list'
mapM_ print polygons
hClose handle
fileToList :: [String] -> [Int]
fileToList = map read
polyList :: Int -> [Int] -> [Polygon]
polyList n [] = []
polyList _ [x] = error "Too few points remaining"
polyList n (v:y:list') =
let pointList = take (2*v) list' -- Note: list' may not *have* 2*v points
points = getPoints pointList
list'' = drop (2*v) list'
-- Calc Perim Here
--Just (under, over) = calcPerim (fromIntegral y) points :: Maybe
(Length, Length)
poly = Polygon { v = v, y = y, points = points}
nextPoly = polyList (n-1) list''
in (poly:nextPoly)
getPoints :: [Int] -> [Point]
getPoints [] = error "Can't create a point (None)"
getPoints [_] = error "Can't create a point (Only One)"
getPoints (k:v:t) = (fromIntegral k, fromIntegral v) : getPoints t
但getPoints正在获得一个空列表。在读入所有顶点之后,最后一对应该是对getPoints的最后一次调用,但似乎它正在使用空列表发送另一个调用。
Polygon {v = 4, y = 2, points = [(0.0,0.0),(4.0,0.0),(4.0,4.0),(0.0,4.0)polycake.hs: Can't create point (None)
CallStack (from HasCallStack):
error, called at polycake.hs:54:16 in main:Main
以下是示例输入:
2
4 2
0 0
4 0
4 4
0 4
6 10
3 15
10 1
12 5
11 19
9 23
6 20
如果第一行是多边形的数量,第二行的第一个值是多边形中的顶点数量,第二个值是计算中使用的值。因此,第二行上的4,2表示4个顶点,将接下来的4行作为顶点对。 6,10创建具有6个顶点等的下一个多边形。
我只是不确定空列表来自哪里来尝试创建点。
答案 0 :(得分:3)
空列表来自:
getPoints (k:v:t) = (fromIntegral k, fromIntegral v) : getPoints t
-- ^^^^^^^^^^^
空列表与模式t
匹配。 (任何列表都匹配模式t
。)
你如何修复它取决于你,但可能最简单的修复(也是一个非常合理的修复)是替换你的基本情况:
getPoints [] = []