lambda中的非穷举模式

tha*_*var 2 haskell

我在lambda中得到了非详尽的模式.我还不确定原因.请任何人如何解决它.代码如下:

import Control.Monad
import Data.List
time_spent h1 h2 = max (abs (fst h1 - fst h2)) (abs (snd h1 - snd h2))
meeting_point xs = foldl' (find_min_time) maxBound xs
where
    time_to_point p = foldl' (\tacc p' -> tacc + (time_spent p p')) 0 xs
    find_min_time min_time p = let x = time_to_point p in if x < min_time  then x else min_time

main = do
  n <- readLn :: IO Int
  points <- fmap (map (\[x,y] -> (x,y)) . map (map (read :: String->Int)) . map words . lines) getContents
  putStrLn $ show $ meeting_point points
Run Code Online (Sandbox Code Playgroud)

GS *_*ica 6

这是具有非详尽模式的lambda : \[x,y] -> (x,y).

非详尽模式是因为您指定的参数[x,y]与任何可能的列表都不匹配 - 它只匹配具有两个元素的列表.

我建议用一个带有错误情况的单独函数替换它,以便在错误消息中打印出意外数据,这样你就可以进一步调试,例如:

f [x,y] = (x, y)
f l = error $ "Unexpected list: " ++ show l

...
points <- fmap (map f . map ...)
Run Code Online (Sandbox Code Playgroud)