Seven Languages in Seven Weeks: Clojure Day 2

Here are the questions for day two:

Using macros and an else condition implement an unless function.

(ns day2q1)

(defmacro unless [test body]
  (list 'cond (list '= true test) (println "") :else body))

(unless true (println "Danger, danger Will Robinson"))
(unless false (println "Do not underestimate the power of the dark side!"))

Using defrecord, write a type that implements a protocol.

(ns day2q2)

(defprotocol Dragon
  (breath [this])
  (fly [this where]))

(defrecord RedDragon [name] 
  Dragon
  (breath [this] (println "Red dragon breathes fire!"))
  (fly [this where] (println "Fly to " where)))

(def Verminthrax (RedDragon. "Verminthrax"))

(breath Verminthrax)
(fly Verminthrax :mystara)

Leave a Reply

Fill in your details below or click an icon to log in:

WordPress.com Logo

You are commenting using your WordPress.com account. Log Out / Change )

Twitter picture

You are commenting using your Twitter account. Log Out / Change )

Facebook photo

You are commenting using your Facebook account. Log Out / Change )

Google+ photo

You are commenting using your Google+ account. Log Out / Change )

Connecting to %s