How do you perform arithmetic calculations on symbols in Scheme/Lisp?

Posted by kunjaan on Stack Overflow See other posts from Stack Overflow or by kunjaan
Published on 2009-07-14T15:31:21Z Indexed on 2010/06/08 21:22 UTC
Read the original article Hit count: 195

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)))

© Stack Overflow or respective owner

Related posts about lisp

Related posts about Scheme