search


My Book of Magic Spells
Wed Mar 14 00:00:00 UTC 2018
Happy π day (3.14)

Today is π day, the day Albert Einstein was born and the day Stephen Hawking died. Coincidence?

To commemorate the life of two great scientists who contributed greatly to humanity's understanding of the universe, I will share with you my book of magic spells

Take these spells and make it your own. Type these spells into Power Turtple REPL Don't copy and paste. Type the spells with your own fingers so you will examine each line. When one function has been entered, test it by calling it before moving to the next function. This gives you immediate feedback and also allows you to isolate bugs if any to that particular function. Make mistakes, fix mistakes, modify it, evolve it, then finally go beyond it. Can you make your own spell to draw a face?


(defn turn-draw
  "turn right by specified angle, then draw a straight line with given length"
  [angle length]
  (right angle)
  (forward length))

(defn polygon
  "draw an n-sided regular polygon with sides of given length"
  [n length]
  (let [angle (/ 360 n)]
    (repeat n #(turn-draw angle length))))

(defn triangle [length]
  (polygon 3 length))

(defn square [length]
  (polygon 4 length))

(defn pentagon [length]
  (polygon 5 length))

(defn hexagon [length]
  (polygon 6 length))

(defn heptagon [length]
  (polygon 7 length))

(defn octagon [length]
  (polygon 8 length))

(defn nonagon [length]
  (polygon 9 length))

(defn hectogon [length]
  (polygon 100 length))

(defn circle [radius]
  (let [n 1000
        fudge-factor 0.01 ;;used instead of doing trig calculations
        distance (* fudge-factor radius)]
    (polygon 1000 distance)))

(defn black-magic
  "What kind of black magic is this?!"
  []
  (doseq [n (range 10)]
    (polygon n 100)))

Practice these spells at home. Question them. Identify what you don't understand. Apply Scientific Method to help you understand. When we meet in class, ask me questions that you were not able to answer yourself. We will practice using these spells and perhaps you can create your own spells to share with the class.

Special prize will be given to students who can create their own black-magic to draw something interesting.