コンテキストでの型の操作 Applicative型クラス:関数をコンテキスト内で使用する <$>と<*>とIOアクション、do表記
入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のUNIT5(コンテキストでの型の操作)、LESSON28(Applicative型クラス:関数をコンテキスト内で使用する)、28.5(練習問題)Q28-2の解答を求めてみる。
コード
type LatLong = (Double, Double)
toRadians :: Double -> Double
toRadians degrees = degrees * pi / 180
latLongToRads :: LatLong -> (Double, Double)
latLongToRads (lat, long) =
let rlat = toRadians lat
rlong = toRadians long
in (rlat, rlong)
haversine :: LatLong -> LatLong -> Double
haversine coords1 coords2 =
let (rlat1, rlong1) = latLongToRads coords1
(rlat2, rlong2) = latLongToRads coords2
dlat = rlat2 - rlat1
dlong = rlong2 - rlong1
a = sin (dlat / 2) ^ 2 + cos rlat1 * cos rlat2 * sin (dlong / 2) ^ 2
c = 2 * atan2 (sqrt a) (sqrt (1 - a))
earthRadius = 3961.0
in earthRadius * c
haversineIO :: IO LatLong -> IO LatLong -> IO Double
haversineIO coords1 coords2 = haversine <$> coords1 <*> coords2
latLongs :: [LatLong]
latLongs =
[ (42.60054, -70.7829),
(42.8250, -70.8150),
(29.9714, -90.7694),
(40.7776, -73.9691)
]
main :: IO ()
main = do
mapM_
( \coords1 ->
mapM_
( \coords2 -> do
x <- haversineIO (return coords1) (return coords2)
print coords1
print coords2
print x
)
latLongs
)
latLongs
入出力結果(Terminal, Zsh)
% runghc sample2.hs
(42.60054,-70.7829)
(42.60054,-70.7829)
0.0
(42.60054,-70.7829)
(42.825,-70.815)
15.602911465598625
(42.60054,-70.7829)
(29.9714,-90.7694)
1408.9424994724843
(42.60054,-70.7829)
(40.7776,-73.9691)
207.19139481857147
(42.825,-70.815)
(42.60054,-70.7829)
15.602911465598625
(42.825,-70.815)
(42.825,-70.815)
0.0
(42.825,-70.815)
(29.9714,-90.7694)
1415.7942372467567
(42.825,-70.815)
(40.7776,-73.9691)
215.50763229827754
(29.9714,-90.7694)
(42.60054,-70.7829)
1408.9424994724843
(29.9714,-90.7694)
(42.825,-70.815)
1415.7942372467567
(29.9714,-90.7694)
(29.9714,-90.7694)
0.0
(29.9714,-90.7694)
(40.7776,-73.9691)
1202.5371978964183
(40.7776,-73.9691)
(42.60054,-70.7829)
207.19139481857147
(40.7776,-73.9691)
(42.825,-70.815)
215.50763229827754
(40.7776,-73.9691)
(29.9714,-90.7694)
1202.5371978964183
(40.7776,-73.9691)
(40.7776,-73.9691)
0.0
%