Last week, I had trouble understanding the last function from the recursion hand-out. The function, i had hard time to figure out is defined as:
(require picturing-programs)
(define (s d)
(cond
[(> d 0)
(beside/align 'bottom
(line 0 (+ 5 (* 2 d)) 'red)
(rotate 90 (s (- d 1))))]
[else (line 0 5 "red")]))
I was playing around with it and started plugging numbers into the functions described above. (s 0) produced just vertical red line that is 5 pixels long. (s 1) produced two lines one laying horizontally in the bottom and the other attached vertically left to this line. Until now, i was just yawing at what functions produced. However, when i plugged (s 30) i was stunned by the result.
Initially, i had to understand where the (line 0 5 "red") comes from. I noticed that this function is derived from the Pythagorean theory; (c^2 = a^2 + b^2) where the two numbers from the function represent the vertical and horizontal component (a and b) of the line.
Now, i now know what this function is comprised of, i had to understand what the function as a whole do.
(s 0) produced one vertical line because of the condition if d is not bigger than zero or represent anything other, it just produces (line 0 5 "red").
(s 1) produced two lines as i described above. This was not surprising because from the portion (line 0 (+ 5 (* 2 d)) 'red), if we plug 1 instead of two, it results in (line 0 7 "red"). Therefore, it aligns a vertical line that is 7 pixels long with a (rotate 90 (s 0)), which is the vertical line that is 5 pixels long but rotated by 90 degrees.
(s 2) produced three lines. I was just taken aback for a moment from wondering where did the extra line came from. Then, i noticed that from the portion (rotate 90 (s (- d 1))), when i plugged the 2 into only this part, it already produced two lines!! Therefore, attaching that to the (line 0 (+ 5 (* 2 (2))) 'red) produced three lines, two vertical and one horizontal.
Therefore i noticed that the function (s d) (where d is a number) rotates the figure produced by (s d-1) by 90 degrees and attach the vertical line to it.
No comments:
Post a Comment