source-academy / sicp

XML sources of SICP and SICP JS, and support for generating Interactive SICP JS, PDF, e-book and comparison editions

Home Page:https://sourceacademy.org/sicpjs

Geek Repo:Geek Repo

Github PK Tool:Github PK Tool

Chapter 1.1.6: Exercise 1.1

Amarunta opened this issue · comments

Hello, as I'm new to programming I've nevertheless decided to propose what a solution for Exercise 1.1 could look like, with the idea that I might perhaps shed light on some of the confusions a fresh student might have or at the very least, clear up some confusions that I might have in the process.

10; - Fairly open and shut, typing in 10; into the interpreter gives back 10 as the Output. This could lead to some confusion, if the person expects the interpreter to receive a command (e.g. print()) from its "master" (in the spirit of the book) in order for one to receive an Output.

The same principle applies to the next few lines of code, so I will skip them for the sake of brevity.

Running const a = 3; and const b = a + 1; through the interpreter returns an undefined value. My hunch is that since there is no actual command given here in the program, there is nothing for the interpreter to interpret and return to the user. Please correct me if I'm wrong

Now here at b > a && b < a * b ? b : a; is where things become very tricky and it took me a bit to understand. As far as the first portion goes, it is very clear - the predicate/condition in this case is b > a && (and) b < a * b. If the predicate is met - then the value that will be expressed by the function will be the one behind door number one, ie. the value behind the question mark. In this case, that would be b ( = a + 1). If the predicate was not fulfilled, then the expression that would be returned would lie behind door number two, ie. the value behind the colon, a ( = 3). The same logic applies to the code after that as well, although a bit more complex:

a === 4
? 6
: b === 4
? 6 + 7 + a
: 25;

If a === (equals) 4, then the value of a changes to 6. If it doesn't, we are lead through to door number two, which offers another predicate (b === 4) with two new choices. If the predicate is not correct (b !== 4), then the interpreter returns the value behind the colon - in this case 25. If it is, then it returns the value behind the question mark, which in this case is 6 + 7 + a and added all together results to 16.

I hope I didn't step over any lines by creating this issue and I especially hope the explanation/solution was helpful to somebody.