3. Functions in Depth
Activity 3.01: Building a Distance and Cost Calculator
Solution:
- Start by defining the
walking-speed
anddriving-speed
constants:(def walking-speed 4) (def driving-speed 70)
- Create two other constants representing two locations with the coordinates
:lat
and:lon
. You can use the previous example with Paris and Bordeaux or look up your own. You will be using them to test your distance and itinerary functions:(def paris {:lat 48.856483 :lon 2.352413}) (def bordeaux {:lat 44.834999Â Â :lon -0.575490})
- Create the distance function. It should take two parameters representing the two locations for which we need to calculate the distance. You can use a combination of sequential and associative destructuring right in the function parameters to disassemble the latitude and longitude from both locations. You can decompose the steps of the calculation in a
let
expression and use theMath/cos
function to calculate the cosine andMath/sqrt
to calculate...