My name is Michael Boutros and I'm a high school student right now. Next year I will study at the University of Waterloo and as a textbook for my CS courses, I took and completed some tasks. The language they use is a circuit, and I learn it when I go. I come from the background of PHP and Ruby, so I'm concerned that some of my habits learned from these languages do not apply correctly to what I do with the circuit. Here is the question from the assignment (available online, by the way):
Write a function called sub-time, which consumes the beginning of the line, representing the time, and the number of natural numbers, which represents the number of minutes to. The function creates a string representing the minutes of minutes to the specified start time.
The string used will be formatted as follows: a two-digit number followed by a “clock”, followed by a two-digit number, and then “minutes”. Time spent on 24 hours format. If the number of hours or minutes is less than 10, then the number will contain 0. The string should be in the following format: number, then “hours”, then number, and then “minutes”. Note that the created line does not have a leading 0 to numbers less than 10. It is possible that the time created may be the time on the previous day.
For instance,
• (sub-time "03 hours 15 minutes" 0) produces "3 hours 15 minutes",
• (sub-time “13 hours 05 minutes” 845) produces “23 hours 0 minutes” and
• (sub-time "13 hours 05 minutes" 2881) produced
13 hours 4 minutes
Using the built-in schema functions string-> number and number-> may be useful.
It is very important that you produce the string exactly as described, otherwise the autotests will fail. In particular, all characters must be lowercase and there must be single spaces between all parts of the string.
Do not use cond expressions in your solution.
(Honestly, I haven't even noticed the last line so far, and my solution really uses conditional expressions. Is there a way to avoid them?)
Here is my solution:
;; Assignment 1 Question 4 (define (convert-to-string hours minutes) (string-append (number->string hours) " hours " (number->string minutes) " minutes")) (define (subtract_hours start_hours sub_hours minutes) (let ((hours (- start_hours (modulo sub_hours 24)))) (if (< hours 0) (convert-to-string (+ 24 hours) minutes) (convert-to-string hours minutes)))) (define (subtract_minutes start_hours start_minutes sub_hours sub_minutes) (let ((minutes (- start_minutes sub_minutes))) (if (< minutes 0) (subtract_hours start_hours (+ 1 sub_hours) (+ 60 minutes)) (subtract_hours start_hours sub_hours minutes)))) (define (sub-time start-time mins) (let ((start_hours (string->number (substring start-time 0 2))) (start_minutes (string->number (substring start-time 9 11))) (sub_hours (quotient mins 60)) (sub_minutes (modulo mins 60))) (subtract_minutes start_hours start_minutes sub_hours sub_minutes)))
I'm incredibly new to Scheme, and although my solution works (from my limited testing), I wonder what some experienced veterans say. Thank you for your time!