Using Haskel language Write a function called flightDistance to compute the appr
ID: 3777573 • Letter: U
Question
Using Haskel language
Write a function called flightDistance to compute the approximate distance between two locations on the Earth (in nautical miles1 ) given the latitude and longitude of each coordinate as Double values.
The needed function is d = a cos1 cos(1)cos(2)cos(1 2) + sin(1)sin(2)
where d is the flight distance2 in miles between two points on a sphere of radius a (in miles), roughly 3963 miles for the Earth. Point one has latitude 1 and longitude 1 while point 2 has latitude 2 and longitude 2. The latitude and longitude need to be in decimal form, i.e. +45.58 latitude and -122.6 longitude is basically 45 degrees North latitude and 122.6 West longitude. Note that these values are in degrees and that trigonometry functions usually assume values in radians. Also, in case you are unfamiliar with the notation, cos1 means an inverse, or arc cosine. Important: Please write your function using tuples to represent each coordinate; write a helper function to convert from degrees to radians. Also you must use a where clause. What is the flight distance between the point at 45N,122W to the point 21N,158W?
Explanation / Answer
import Text.Printf
hsin t = let u = sin (t/2) in u*u
distRad radius (point1_lat, point1_lng) (point1_lat, point1_lng) =
let hlat = hsin (point2_lat - point1_lat)
hlng = hsin (point2_lng - point1_lng)
root = sqrt (hlat + cos point1_lat * cos point2_lat * hlng)
in 2 * radius * asin (min 1.0 root)
distDeg radius p1 p2 = distRad radius (deg2rad p1) (deg2rad p2)
where deg2rad (t, u) = (d2r t, d2r u)
d2r t = t * pi / 180
earthDist = distDeg 6372.8
main = do
let point1 = (36.12, -86.67)
point2 = (33.94, -118.40)
distance = earthDist point1 point2 :: Double
printf "The distance between Point 1 and Point 2 is: %0.f km. " distance
Related Questions
drjack9650@gmail.com
Navigate
Integrity-first tutoring: explanations and feedback only — we do not complete graded work. Learn more.