列表内相邻项的差值
有这样一道题,有一组数字 245 84 180 230 360 ,问那两个数字相差最小?
比较常见的算法是排序然后求相邻两个数字之间的差。
plusList :: Num a => [a] -> [a] plusList [] = [] plusList (x:[]) = [x] plusList (x:y:[]) = [y-x] plusList (x:y:xs) = (y-x):(plusList $ y:xs)
*Main Data.List> sort [245,84,180,230,360]
[84,180,230,245,360]
*Main Data.List> plusList $ sort [245,84,180,230,360]
[96,50,15,115]
测试一下