D&C GLug - Home Page

[ Date Index ] [ Thread Index ] [ <= Previous by date / thread ] [ Next by date / thread => ]

[LUG]Deg-deci to Deg-Min-Sec prg improved (Lisp)

 

Hi there

For what it's worth, reckon have improved that program.
Same output, but in a "purer" way.

Separated the computation from the human-readable presentation.

Have
"abs-degdeci-to-abs-deg-min-sec"
fn. returning a list comprising the numeric Degrees, the Minutes and
the Seconds.
eg.  (123 45 67.8) == 123Deg 45Seconds 67.8Seconds
{"Lisp" == "List processing" - so fundamentally right to return a list}
So that output could be used by any calling function for any other
purpose as yet unforeseen.
Test example:
(abs-degdeci-to-abs-deg-min-sec 123.456789)
;; (123 27 24.440400000002)
ie. that fn. has become a general-purpose function for converting
Deg-decimal to Degrees-Minutes-Seconds, "agnostic" to
end-application.

With all conversion to human-readable output for nautical purposes
isolated in the inherently nautical "top-level" user-interaction
calling function
"lat-long-degdeci-to-dms"

I discovered the neat "nth" function in emacs lisp.
Apparently it's written into the interpretter in "C" so very fast.

(car llat) has the same effect as (nth 0 llat)

Damn' thing is, with no "Gee-whizz this is a great new language!!"
there isn't as far as I know the books showing how to apply this
established language (?)

As std. with Lisp, you start out with the bottom-most "cutting-tip"
function and work your way to the user-level function.
(opposite order to "top down" "usual" programming)

---------------- listing ----------------

;;; When copying-and-pasting "Google Maps" degdeci lat. and long.,
;;; delete the comma between the values !!!


(defun abs-degdeci-to-abs-deg-min-sec (absdegdeci)
  "Deg-decimal to DMS format output - return list
For navigation this can be called for N-S and W-E, so cannot know what
  to call the returned value"
  (if (minusp absdegdeci)
      "error - cannot handle negative arguments"
    (let ((decix60 (* (mod absdegdeci 1) 60)))
      (list (truncate absdegdeci) (truncate decix60) (* (mod decix60 1) 60)))))

(defun lat-long-degdeci-to-dms (latdeci longdeci)
  (let
      ((llat (abs-degdeci-to-abs-deg-min-sec (abs latdeci)))
       (llong (abs-degdeci-to-abs-deg-min-sec (abs longdeci))))
    (format "%03dd %02d' %04.1f'' %s   %03dd %02d' %04.1f'' %s"
            (car llat)(nth 1 llat)(nth 2 llat)(if (minusp latdeci) 'S 'N)
            (car llong)(nth 1 llong)(nth 2 llong)(if (minusp longdeci) 'W 'E))))

---------------- example ----------------

;;; Berry Head lighthouse
;;; Google :
;;; 50.39954886056384, -3.483553379652956
(lat-long-degdeci-to-dms 50.39954886056384 -3.483553379652956)
;; "050d 23' 58.4'' N   003d 29' 00.8'' W"

;;; "paste"
;;; 050 23' 58.4" N   003 29' 00.8" W
;;; into Google Maps gets you back to Berry Head lighthouse

--
The Mailing List for the Devon & Cornwall LUG
FAQ: https://www.dcglug.org.uk/faq/