計算機科学のブログ

Haskell - コンテキストでの型の操作 - Applicative型クラス:関数をコンテキスト内で使用する - IO

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

コード

sample.hs

main :: IO ()
main = do
  d <- haversineIO x y
  print d

type LatLong = (Double, Double)

toRadians :: Double -> Double
toRadians degrees = degrees * pi / 180

latLongToRads :: LatLong -> (Double, Double)
latLongToRads (lat,long) = (rlat,rlong)
    where rlat = toRadians lat
          rlong = toRadians long

haversine :: LatLong -> LatLong -> Double
haversine coords1 coords2 = earthRadius * c
    where (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-1))
          earthRadius = 3961.0

haversineIO :: IO LatLong -> IO LatLong -> IO Double
haversineIO val1 val2 = do
    v1 <- val1
    v2 <- val2
    return $ haversine v1 v2

x :: IO LatLong
x = return (2, 3)
y :: IO LatLong
y = return (4, 5)

入出力結果(Terminal, Zsh)

% runghc sample.hs 
12443.848500869171
%