(require 'clojure.walk)
(clojure.walk/macroexpand-all (quote (func)))
This seems to work quite well. Another option is to use let to define everything.
Showing posts with label problems. Show all posts
Showing posts with label problems. Show all posts
Thursday, May 26, 2011
Solving 4Clojure Problems
I have been solving problems on the 4clojure website. In order to solve the problems on this site I used clojure.walk/macroexpand-all and I defined all my functions as macros in order to get past the def problem.
Friday, May 6, 2011
Open Computer Science Problems
I found this new problems site. The first problem is simple:
(map
(fn [i]
(Math/pow 3 (dec i)))
(range 1 16)))
The second problem isn't so simple. I implemented the procedure the site described, however, it doesn't work because the range is way too large:
(let [n (fn [i]
(count (filter #(= \1 %) (Integer/toString i 2))))]
(map n (range 1 (inc (Math/pow 2 50)))))
Instead I solved this problem using mathematical simplification which lead me to:
(inc (* 50 (Math/pow 2 49)))
Then I got to the third problem. Apparently they want me parse some expression! I am a Lisp programmer for a reason: I don't like to parse. So instead I made a macro based upon this problem:
(defmacro ternary-expression
[if-true expr if-false]
`(if ~expr
~if-true
~if-false))
Subscribe to:
Posts (Atom)