Haskell - コンテキストでの型の操作 - Applicative型クラス:関数をコンテキスト内で使用する - Functorの制限
入門Haskellプログラミング (Will Kurt(著)、株式会社クイープ(監修、翻訳)、翔泳社)の UNIT5(コンテキストでの型の操作)、LESSON 28(Applicative型クラス:関数をコンテキスト内で使用する)、28.1(2つの都市の距離を計算するコマンドラインアプリケーション)、クイックチェック28-2の解答を求めてみる。
コード
sample2.hs
main :: IO ()
main = do
print $ distanceFromNY tokyo
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
newYork :: LatLong
newYork = (1, 2)
distanceFromNY :: LatLong -> Double
distanceFromNY = haversine newYork
tokyo :: LatLong
tokyo = (3, 4)
入出力結果(Terminal, Zsh)
% runghc sample2.hs
12443.848500869171
%