views:

386

answers:

2

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)))
+1  A: 

you need to convert to numerical values for your calculations to make sense. (hour (string->number (string (first a-time)))) same for minute

SpaceghostAli
(first a-time) will be a character . im stuck.
kunjaan
oh well how about (char->integer ...)
SpaceghostAli
char-integer converts it to unicode value.
kunjaan
ok yea sorry again no more being lazy (string->number (string (first a-time))) will give you what you want
SpaceghostAli
Felleisen would be disgusted with me (I'm a NEU allum)
SpaceghostAli
+4  A: 

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)))
Nathan Kitchen
Whoops, forgot the link. It's added now.
Nathan Kitchen
Since the question was tagged with "plt-scheme", you can use (regexp-split #rx":" time) -- no need for the string-split code.
Eli Barzilay