During this activity, students should be able to:
This activity helps the student develop the following skills, values and attitudes: ability to analyze and synthesize, capacity for identifying and solving problems, and efficient use of computer systems.
Individually, solve the following set of meta-programming exercises using Clojure macros.
Place all your functions in a name space called macros
.
This is the documentation for Clojure's or
macro:
or
macro
Usage:
(or)
(or x)
(or x & next)
Evaluates its expressions one at a time, from left to right. If a form returns a logical true value, it returns that value and doesn't evaluate any of the other expressions, otherwise it returns the value of the last expression. (or) returns nil.
Write a macro called my-or
that works as described above. Make sure that any expression gets evaluated at most once. You may not use Clojure's or
macro. Usage examples:
(macros/my-or) ⇒ nil (macros/my-or false :one nil :two false :three) ⇒ :one (macros/my-or false false nil) ⇒ nil (macros/my-or nil nil false) ⇒ false
There are situations where we want to randomly pick a path of execution. Such a requirement might arise if we were, for instance, simulating the behavior of an ant, and wanted to introduce some randomness into it.
Write a macro called randomly
that accepts one or more expressions, picks one at random and evaluates that one only. It should return the result of the evaluated expression. Usage examples:
(macros/randomly 1 2 3 4) ⇒ 3 ;;; (results may vary randomly, duh!) (macros/randomly (println 'athos) (println 'porthos) (println 'aramis)) ;;; prints athos ⇒ nil (macros/randomly (do (println "dona") :one) (do (println "nobis") :two) (do (println "pacem") :three)) ;;; prints nobis ⇒ :two
Write a macro called infix
that takes as its argument an expression using infix notation as depicted in the following context-free grammar:
‹expression› → ‹number›
‹expression› → ( ‹expression› ‹operator› ‹expression› )
‹operator› → + | − | * | /
The macro must transform its input expression into Clojure's prefix notation, which is then evaluated into the final result. Usage examples:
(macroexpand-all '(macros/infix 42)) ⇒ 42 (macroexpand-all '(macros/infix (1 + 2))) ⇒ (+ 1 2) (macroexpand-all '(macros/infix (((1 + 2) * (3 - 4)) / 5))) ⇒ (/ (* (+ 1 2) (- 3 4)) 5) (macros/infix 42) ⇒ 42 (macros/infix (1 + 2)) ⇒ 3 (macros/infix (((1 + 2) * (3 - 4)) / 5)) ⇒ -3/5
Write a macro called do-loop
that implements a post-test loop control statement. It must combine the functionality of C's do-while
statement and Pascal's repeat-until
statement.
This construct consists of a body (one or more expressions, presumably with side effects) and a final conditional form prefaced with a :while
or :until
keyword. First, the expressions in the body are evaluated sequentially, and then the condition is evaluated. If the final form uses a :while
keyword, the body of the loop is repeated while the condition holds true. On the other hand, if the final form uses an :until
keyword, the body of the loop is repeated while the condition holds false (or in other words, the loop terminates when the condition is true). Returns nil
.
Here are some examples:
(def i) (binding [i 0] (macros/do-loop (println i) (set! i (inc i)) (:until (= i 5)))) ;;; prints: ;;; 0 ;;; 1 ;;; 2 ;;; 3 ;;; 4 ⇒ nil (def j) (binding [j 1] (macros/do-loop (println j) (set! j (inc j)) (:while (<= j 5)))) ;;; prints: ;;; 1 ;;; 2 ;;; 3 ;;; 4 ;;; 5 ⇒ nil
Using the Online Assignment Delivery System (SETA), deliver the file called macros.clj
containing your solutions. No assignments will be accepted through e-mail or any other means.
IMPORTANT: The program source file must include at the top the author's personal information (name and student id) within comments. For example:
;;; ITESM CEM, November 29, 2011. ;;; Clojure Source File ;;; Activity: Macros ;;; Author: Steve Rogers, 1166611 . . (The rest of the program goes here) .
Due date: Tuesday, November 29.
This activity will be evaluated using the following criteria:
-10 | The program doesn't contain within comments the author's personal information. |
---|---|
10 | The program contains syntax errors. |
DA | The program was plagiarized. |
10-100 | Depending on the amount of exercises that were solved correctly. |