I'm a beginning programmer. I started with Scheme, as I hear it's fairly easy to start with, and it has a linux shell. I wrote this program which takes the full MM/DD/YEAR, and returns the day of the week. It was an excercise from my book (but no they don't give you any code). Everything works wonderfully, in both MzScheme and MrEd. My only concern is that the code is too wordy, and not near as effecient as it could/should be. If anyone would be willing to look through and suggest ways I could clean up my coding, on this project or in general, it would be much appreciated.
Thanks in advance,
Code:
;
;Day of the Week
;Freely redistributable (if anyone actually wants it :P)
;
(define writeln ;writes string to the screen
(lambda args
(for-each display args)
(newline)))
(display "Welcome to Hell... please wipe your feet.")
(newline)
(display "I'm going to tell you what day of the week it is on any given day.")
(newline)
(display "Impressive huh?")
(newline)
(define weekday
(lambda ()
(display "Enter date (MM/DD/YEAR)")
(newline)
(display "Type quit to quit.")
(newline)
(let ((response (read))) ;reads from input
(if (not (symbol? response)) (error "invalid entry") ;ensures input symbol
(set! response (symbol->string response))) ;convert to string
(cond
((equal? response "quit") ;exit door procedure
(Display "Yes sir, no sir, okay, thank you, mabye, goodbye."))
((equal? (testdate response) #f) ;verifies valid date
(error "invalid entry")
(weekday))
(else
(let ((x (convert-day-week response))) ;calls on the formula
(writeln response " is a " (day-to-day x))
(newline))
(weekday))))))
(define testdate ;helper program to determine if the input is valid
(lambda (inpdat)
(if
(or (not (equal? 10 (string-length inpdat)))
(not (number? (and (getmonth inpdat)
(getdate inpdat)
(getyear inpdat))))
(or (> (getmonth inpdat) 12) (< (getmonth inpdat) 1))
(or (> (getdate inpdat) 31) (< (getdate inpdat) 1))
(or (not (equal? (substring inpdat 2 3) "/"))
(not (equal? (substring inpdat 5 6) "/"))))
#f
#t)))
(define getmonth ;helper program to retrieve and convert month
(lambda (inpdat)
(string->number (substring inpdat 0 2))))
(define getdate ;helper program to retrieve date
(lambda (inpdat)
(string->number (substring inpdat 3 5))))
(define getcent ;helper program to retrieve last century
(lambda (inpdat)
(string->number (substring inpdat 6 8))))
(define getyear ;helper program to retrieve year
(lambda (inpdat)
(string->number (substring inpdat 8 10))))
(define convert-month ;converts to the formula specific month
(lambda (month)
(if (< month 3)
(+ 10 month)
(- month 2))))
(define convert-day-week ;the meat and potatos
(lambda (response) ;the mathematical formula
(modulo (- (+
(truncate (/ ( - (* 13 (convert-month (getmonth response))) 1) 5))
(truncate (/ (getyear response) 4))
(truncate (/ (getcent response) 4))
(getdate response)
(getyear response))
(* 2 (getcent response))) 7)))
(define day-to-day ;converts convert-day-week result into a
(lambda (x) ;string name result
(cond
((= x 0) "Sunday")
((= x 1) "Monday")
((= x 2) "Tuesday")
((= x 3) "Wednesday")
((= x 4) "Thursday")
((= x 5) "Friday")
((= x 6) "Saturday")
(else (error "Unexpected error")))))
(weekday)