Skip to content

Latest commit

 

History

History
25 lines (17 loc) · 2.22 KB

1.md

File metadata and controls

25 lines (17 loc) · 2.22 KB

Day 4

Didn't have much time for this today so all I did was edit my Tip Calculator from yesterday to use local variables like Tim Macdonald suggested. I had no idea that defvar was only for global variables. I thought what made a variable global was surrounding the symbol name with asterisks. It wasn't working with his code (the let binding for the tip variable was malformed). Fixed that so I was no longer getting an error from the compiler. The code looked like this:

(let ((tip (get-tip price percentage))
        (total (+ price tip)))
    (format t "Your tip is $~a.~%Your total is $~a.~%" tip total)))

I was still getting warnings stating tip wasn't found though. I tried to run it anyway and it kept crashing because tip hadn't been declared yet in the compiler's eyes. The only way I was able to fix this was by removing the tip variable completely and just passing the result from the evaluation of (get-tip price percentage) directly into the calculation of the total. So the function ended up looking like this:

(defun calculate-tip-and-total (price percentage)
    (let ((total (+ price (get-tip price percentage))))
        (format t "Your tip is $~a.~%Your total is $~a.~%" (get-tip price percentage) total)))

If anyone knows how to make it so I can refer to one local variable with another, please let me know.

It was also pointed out to me on Twitter by Rainer Joswig and on GitHub by vindarel that there is actually a library for parsing floats out of strings. Didn't implement it in the Tip Calculator today, but maybe another day.

Also, I didn't see this until today, but Michael Herda gave me a great explanation of what constitutes the difference between forms and non-forms in Common Lisp. It really cleared it up and made the difference feel more concrete for me.

Code for today's post is in code/Dec2018/1.