The Boring Stream - #007
In #TheBoringStream I spent part of the night working on some exercises using #Lisp. (Exercises 1.1 - 1.5)
So far I find it pretty easy to understand the contents of the book and the language is very easy to pick up, syntax-wise.
The only thing that threw me off during this first batch of exercises was guessing the difference between how applicative/normal-order interpreters will evaluate the following code:
(define (p) (p)) ;; p will return itself?
(define (test x y)
(if (= x 0) 0 y))
(test 0 (p))
After spinning my wheels for about 5 minutes I realized that the difference is that when x = 0, normal-order never needs to evaluate (p) in the test, so will return the result 0. Whereas with applicative interpretation (p) will always get evaluated and because it returns itself it creates an infinite loop.
This was tricky because Lisp didn't throw any sort of runtime error or yell at me, so without debugging it's kinda tricky to know exactly whats going on.
In Dr.Racket (the IDE for LISP/Scheme) there was a subtle signifier that something was odd about the code when it ran. Upon closer inspection there was some weird indicator flickering extremely fast (which I guess is a way to indicate that the runtime is having to offload a lot of memory very quickly).
Turns out this area of the UI shows the activity of the Garbage Collector.
Another question that was interesting was:
Define a procedure that takes three numbers as arguments and returns the sum of the squares of the two larger numbers.
Here was my solution:
(define (myFinalFun a b c)
(cond
((and
(< (+ a c) (+ b c))
(> (+ b c) (+ a b)))
(sum (square b) (square c))) ; Handles for if b, c is largest combo
((and
(> (+ a c) (+ b c))
(> (+ a c) (+ a b)))
(sum (square a) (square c))) ; handles if a, c is the largest
(
(sum (square a) (square b))) ; fall-through for a, b
))
This isn't the most optimal solution, but it's what i came up with on my own- and I'm totally proud of it.
???? A better solution can be found here (spoiler)
If you wanna see the SUPER BORING 2hr stream where I worked on these and other things from Chapter 1 of Structure and Interpretation of Computer Programs, check out the link below:
?? Watch Stream:?https://lnkd.in/e6PnBiK6
?? Repo:?https://lnkd.in/eGCrhsSm