Tuesday, March 11, 2014

CSC 7-- Recursion

    This week, we talked more about the recursion. After taking the lecture in the morning, I did some practices in Dr. racket to be familiar with the recursion function. One of the practices is so classical in the Computer Science that it has four arguments in the function. I think it is complicated but I deal with the difficulties by writing out all the step one by one in the Dr. Racket. After listing all the steps, I can explain each step easier than doing it directly with the complicated function. Here is an easy example I did as my practice.

(require 2htdp/image)
(define serp0
  (circle 15 "solid" "green"))

(define (serp d)
  (if
   (zero? d)
   serp0
   (above
    (serp (sub1 d))
          (beside
           (serp (sub1 d))
           (serp (sub1 d))))))
it will produce as followed: 
Personally, the best way to understand the complicated function is to do it again by your own and change some arguments in the function. Here is a classical and complicated example.
(define (k d)
  (cond
    [(> d 0)
     (beside/align
      "bottom"
      (k (- d 1))
      (rotate 60 (k (- d 1)))
      (rotate -60 (k (- d 1)))
      (k (- d 1)))]
    [else (line 5 0 "blue")]))
This week, we will begin to do some programming project. I hope I can share some thoughts with you guys in this blog when I begin to touch the assignment. 

No comments:

Post a Comment