Reputation: 53481
I need to perform calculations with a symbol. I need to convert the time which is of hh:mm form to the minutes passed.
;; (get-minutes symbol)->number
;; convert the time in hh:mm to minutes
;; (get-minutes 6:19)-> 6* 60 + 19
(define (get-minutes time)
(let* ((a-time (string->list (symbol->string time)))
(hour (first a-time))
(minutes (third a-time)))
(+ (* hour 60) minutes)))
This is an incorrect code, I get a character after all that conversion and cannot perform a correct calculation.
Do you guys have any suggestions? I cant change the input type.
Context: The input is a flight schedule so I cannot alter the data structure.
;; ----------------------------------------------------------------------
Edit: Figured out an ugly solution. Please suggest something better.
(define (get-minutes time)
(let* ((a-time (symbol->string time))
(hour (string->number (substring a-time 0 1)))
(minutes (string->number (substring a-time 2 4))))
(+ (* hour 60) minutes)))
Upvotes: 2
Views: 1353
Reputation: 4867
You can find a definition for string-split here. It will enable you to split a string at delimiters of your choice. Then you can define get-minutes like this:
(define (get-minutes time)
(let* ((fields (string-split (symbol->string time) '(#\:)))
(hour (string->number (first fields)))
(minutes (string->number (second fields))))
(+ (* hour 60) minutes)))
Upvotes: 4
Reputation: 8392
you need to convert to numerical values for your calculations to make sense. (hour (string->number (string (first a-time)))) same for minute
Upvotes: 1