gpt4 book ai didi

haskell - 如何使用数据列表比较并返回数据

转载 作者:行者123 更新时间:2023-12-04 02:37:00 24 4
gpt4 key购买 nike

我是 Haskell 的新手,我正在努力寻找一种使用类成员变量来返回我正在寻找的成员变量的方法。我有这个数据:

 data Place = Place {name :: String, 
north :: Float,
east :: Float,
rainfall :: [Int]
} deriving (Eq, Ord, Show)

testData :: [Place]
testData = [
Place "London" 51.5 (-0.1) [0, 0, 5, 8, 8, 0, 0],
Place "Norwich" 52.6 (1.3) [0, 6, 5, 0, 0, 0, 3],
Place "Birmingham" 52.5 (-1.9) [0, 2, 10, 7, 8, 2, 2],
Place "Hull" 53.8 (-0.3) [0, 6, 5, 0, 0, 0, 4],
Place "Newcastle" 55.0 (-1.6) [0, 0, 8, 3, 6, 7, 5],
Place "Aberdeen" 57.1 (-2.1) [0, 0, 6, 5, 8, 2, 0],
Place "St Helier" 49.2 (-2.1) [0, 0, 0, 0, 6, 10, 0]
]

我想做的是返回最接近给定位置的地方。到目前为止,我能够计算每个地方到给定位置的距离,并且我确切地知道应该返回哪个项目,但我不知道如何实际执行此操作。这是我到目前为止的代码;

closestDry :: Float -> Float -> [Place] -> [Float]
closestDry _ _ [] = []
closestDry lx ly (x:xs) = distance(lx)(ly)(north x)(east x)):closestDry lx ly xs

distance :: Float -> Float -> Float -> Float -> Float
distance x1 y1 x2 y2 = sqrt ((y1 - y2)^2 + (x1 - x2)^2)

在控制台中输入“closestDry 51.5 (-0.1) testData”输出:

[0.0,1.7804484,2.059126,2.3086786,3.8078866,5.946426,3.0479496] 

我可以看到,按照给定地点列表的顺序,最近的区域必须是“伦敦”,因为距离为“0.0”,但是如何将这个地点返回给我?

我不想返回距离列表,但我不知道如何告诉函数获取最小距离并返回相应的地点,因为它需要与其他地点进行比较。

最佳答案

closestDry 基本上是无用的困惑,所以摆脱它。然后,让我们编写一个 distanceTo 函数,用于给出从坐标到某个地点的距离:

distanceTo :: Float -> Float -> Place -> Float
distanceTo lat lon place = distance lat lon (north place) (east place)

现在,让我们编写一个函数,将地点与到它们的距离配对:

distancesTo :: Float -> Float -> [Place] -> [(Place, Float)]
distancesTo lat lon = map (\place -> (place, distanceTo lat lon place))

尝试一下:

λ> distancesTo 51.5 (-0.1) testData
[(Place {name = "London", north = 51.5, east = -0.1, rainfall = [0,0,5,8,8,0,0]},0.0),(Place {name = "Norwich", north = 52.6, east = 1.3, rainfall = [0,6,5,0,0,0,3]},1.7804484),(Place {name = "Birmingham", north = 52.5, east = -1.9, rainfall = [0,2,10,7,8,2,2]},2.059126),(Place {name = "Hull", north = 53.8, east = -0.3, rainfall = [0,6,5,0,0,0,4]},2.3086786),(Place {name = "Newcastle", north = 55.0, east = -1.6, rainfall = [0,0,8,3,6,7,5]},3.8078866),(Place {name = "Aberdeen", north = 57.1, east = -2.1, rainfall = [0,0,6,5,8,2,0]},5.946426),(Place {name = "St Helier", north = 49.2, east = -2.1, rainfall = [0,0,0,0,6,10,0]},3.0479496)]

到目前为止看起来不错!

现在我们可以使用 minimumBycomparingsnd 来获取元组,然后使用 fst 提取该位置:

import Data.Foldable (minimumBy)
import Data.Ord (comparing)

closestTo :: Float -> Float -> [Place] -> Place
closestTo lat lon places = fst $ minimumBy (comparing snd) (distancesTo lat lon places)

让我们尝试一下:

λ> closestTo 51.5 (-0.1) testData
Place {name = "London", north = 51.5, east = -0.1, rainfall = [0,0,5,8,8,0,0]}

成功!


作为 distancesTo 的替代方案,您还可以使用比较来计算距离,如下所示:

closestTo :: Float -> Float -> [Place] -> Place
closestTo lat lon places = minimumBy (comparing (distanceTo lat lon)) places

这样做的优点是不需要任何元组,但缺点是需要多次重新计算同一位置的距离。


无论哪种方式都要注意:minimumBy是一个危险的部分函数,​​如果它得到一个空列表,它将使你的程序崩溃,如果closestTo得到一个空列表,就会发生这种情况列表:

λ> closestTo 51.5 (-0.1) []
*** Exception: Prelude.foldl1: empty list

如果您关心这一点,则需要通过返回 Maybe Place 来避免这种情况,并调整代码以在输入列表为空时返回 Nothing ,而不是调用 minimumBy。 (IMO,这是 Haskell 中的一个缺陷,minimumBy 应该只返回一个 Maybe 本身,而不必崩溃。)

关于haskell - 如何使用数据列表比较并返回数据,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/61262801/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com