LinuxQuestions.org
Support LQ: Use code LQ3 and save $3 on Domain Registration
Go Back   LinuxQuestions.org > Forums > Non-*NIX Forums > Programming
User Name
Password
Programming This forum is for all programming questions.
The question does not have to be directly related to Linux and any language is fair game.

Notices

Reply
 
LinkBack Search this Thread
Old 10-31-2004, 05:24 AM   #1
audibel
Member
 
Registered: Oct 2004
Distribution: Debian Sarge
Posts: 177

Rep: Reputation: 30
Some scheme code... any suggestions?


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)

Last edited by audibel; 11-01-2004 at 02:49 AM.
 
Old 11-10-2004, 11:05 PM   #2
Peterius
Member
 
Registered: May 2004
Distribution: Gentoo, Debian, OpenBSD, NetBSD
Posts: 155

Rep: Reputation: 16
Its wordy and looks like that because its scheme. What the hell are you using scheme for? Learn C. Its easy, will help you learn other languages later, will structure your mind in the right way, and is a good imperative language. (Computers are imperative by nature) Scheme is a functional language as I recall. Which means its useless and will get you in to bad habits. Maybe you want to learn a functional language AFTER you have imperative languages down, just to shake things up a bit. But not now.

By the way, if your learning scheme in some sort of Computer Science 101 class in college.. transfer.
 
  


Reply


Thread Tools Search this Thread
Search this Thread:

Advanced Search

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is Off
HTML code is Off
Trackbacks are Off
Pingbacks are On
Refbacks are Off


Similar Threads
Thread Thread Starter Forum Replies Last Post
small syntax problem with C code (implemented in Code Composer Studio) illiniguy3043 Programming 6 01-07-2008 02:14 AM
User Preferences: Use HTML code instead of vB code? (vB code is overrated) stefanlasiewski LQ Suggestions & Feedback 5 07-26-2005 01:37 AM
Editing buttons (quote, code etc.) add the code to the end vharishankar LQ Suggestions & Feedback 2 09-13-2004 09:32 AM
Diffrerence between position independent code and Relocatable code? eshwar_ind Programming 7 05-11-2004 01:40 AM
Open Firmware code for booting OS from SATA : sample code available somewhere ? drsparikh Linux - Hardware 0 03-12-2004 11:16 AM


All times are GMT -5. The time now is 02:53 PM.

Main Menu
 
My LQ
Write for LQ
LinuxQuestions.org is looking for people interested in writing Editorials, Articles, Reviews, and more. If you'd like to contribute content, let us know.
Main Menu
Syndicate
RSS1  Latest Threads
RSS1  LQ News
Twitter: @linuxquestions
identi.ca: @linuxquestions
Facebook: @linuxquestions
Open Source Consulting | Domain Registration