計算機科学のブログ

コンテキストでの型の操作 Applicative型クラス:関数をコンテキスト内で使用する <*>を使わない場合 IOアクション、do表記

入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)のUNIT5(コンテキストでの型の操作)、LESSON28(Applicative型クラス:関数をコンテキスト内で使用する)、28.5(練習問題)Q28-1の解答を求めてみる。

コード

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 - rlong2
      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

latLongs :: [LatLong]
latLongs =
  [ (42.60054, -70.7829),
    (42.8250, -70.8150),
    (29.9714, -90.7694),
    (40.7776, -73.9691)
  ]

haversineIO :: IO LatLong -> IO LatLong -> IO Double
haversineIO v1 v2 = do
  latLong1 <- v1
  latLong2 <- v2
  return $ haversine latLong1 latLong2

main :: IO ()
main = do
  mapM_
    ( \latLong1 ->
        mapM_
          ( \latLong2 -> do
              v <-
                haversineIO
                  (return latLong1)
                  (return latLong2)
              print latLong1
              print latLong2
              print v
          )
          latLongs
    )
    latLongs

入出力結果(Terminal, Zsh)

% runghc sample1.hs
(42.60054,-70.7829)
(42.60054,-70.7829)
0.0
(42.60054,-70.7829)
(42.825,-70.815)
15.517479080584357
(42.60054,-70.7829)
(29.9714,-90.7694)
873.0839158681493
(42.60054,-70.7829)
(40.7776,-73.9691)
126.02438436763566
(42.825,-70.815)
(42.60054,-70.7829)
15.517479080584357
(42.825,-70.815)
(42.825,-70.815)
0.0
(42.825,-70.815)
(29.9714,-90.7694)
888.6013949487335
(42.825,-70.815)
(40.7776,-73.9691)
141.54186344822003
(29.9714,-90.7694)
(42.60054,-70.7829)
873.0839158681493
(29.9714,-90.7694)
(42.825,-70.815)
888.6013949487335
(29.9714,-90.7694)
(29.9714,-90.7694)
0.0
(29.9714,-90.7694)
(40.7776,-73.9691)
747.0595315005136
(40.7776,-73.9691)
(42.60054,-70.7829)
126.02438436763566
(40.7776,-73.9691)
(42.825,-70.815)
141.54186344822003
(40.7776,-73.9691)
(29.9714,-90.7694)
747.0595315005136
(40.7776,-73.9691)
(40.7776,-73.9691)
0.0
%