Chicken, Eggs, and a Matter of Time

In the past several years when I have been doing any hobbyist programming, I usually chose to use Racket as my language of choice. I chose Racket as I was getting interested in Lisp styled languages, and Racket is a great place to start down that path. But as time has gone on I wanted to try and learn a more pure scheme langauge. Yes, Racket is based upon scheme, but it really has become its own thing now. So down the scheme rabbit hole I fell... After looking at the main scheme interperations out there, it quickly became a toss up between:

I intially chose Guile, but ultimately settled on Chicken. A large reason for this is the documentaiton. Guile's documentation is quite simply atrocious, it is very difficult to use/understand. Chicken on the other hand has quite good documentation. I also do not like that you have to use the Guix package manager to install extensions for Guile. There may be another way, but I lost interest to keep looking. But ultimately this little page sold me on Chicken. Now, I am a firm believer that the best way to learn a new language is to work on a project. Sure, the first language you learn you should be doing a lot of exercises and going through a lot of learning material. After your first langauge though, you can go over a quck tutorial and start working on something when picking up a bew language. At my current job we just switched to a new time clock software, and it is lacking to say the least. One of the features it is lacking is showing your total number of hours, so you have to total them all up manually and calcualate it out. Not to mention that putting time in a calcalutor won't work, for example: 6:45 + 7:23 means nothing to a normal calculator. So you both need to convert it and do the math manually, ughhh... Well this provides an example for writing a crude yet working script. With this software being all closed source there really is no way for me to scrape the times and everyhting be totally automated, but I can at least create a script that takes the times as input and do the coversions and math by itself. A good little project to get my feet wet with a new language. The first thing I had to figure out was getting the Chicken REPL set up. The main pain initially is that readline is not enabled by default, so you can not use the arrow keys to navigate history, well that is a pain, but easily remidied. All you need to do is install the linenoise chicken egg, which on Debian based distros is this single command: sudo chicken-install linenoise. After that, create ~/.csirc and add this text into it:

(import linenoise)
(current-input-port (make-linenoise-port))
Now evertime you load the REPL via csi you will have readline ability. Sweet! Now that chicken is set up, I went a head and followed this tutorial, which did not take me that long. After this, I was ready to attack the problem. Now I won't go into all the difficulites I had with this script, but it did take me a few hours to get it working fully. Mainly because writing things in scheme is quite the mental exercise, defintely a challenge but a fun and rewarding one! The one thing I was not able to implemenmt was getting clean input for the time from the user, so for now you just need to edit the times in a list and run the file. Here is the script below:
(define (hours-to-minutes hours)
   (* (apply + (map car hours)) 60))

(define (total-minutes minutes)
   (apply + (map cadr minutes)))

(define (total-time lst)
   (/ (+ (hours-to-minutes lst)(total-minutes lst)) 60.0))

(display "Total Hours: ")

(print (total-time
   '((7 57) ; 7:57 = 7 57, just replace : with a space
    (7 55)
    (8 3)
    (8 25)
    (7 49)
    (7 59)
    (8 3)
    (8 48)
    (8 9)
    (7 56))))
Now all the user needs to do is edit the times in the list, and run: csi -s timesheet.scm and the total time will be returned. The above examples prints: Total Hours: 81.0666666666667. I was a bit over on this pay period, but now I can see the toal hours so this was a success! Well as Dave Chappelle once said, "Zip it up and zip it out!"
Home