Which power function is right for Clojure?

(ns power.examples)

(defn non-acc-pow [base exp]
  (if (zero? exp)
    (* base (non-acc-pow base (dec exp)))))

(defn acc-pow [base exp]
  (letfn [(loop [base exp acc]
            (if (zero? exp)
              (recur base (dec exp) (* base acc))))]
    (loop base exp 1)))

(defn lazy-pow [base exp]
  (letfn [(loop [cur]
            (cons cur (lazy-seq (loop (* cur base)))))]
    (nth (take exp (loop base)) (dec exp))))

(defn iter-pow [base exp]
  (nth (take exp (iterate (partial * base) base)) (dec exp)))

(defn apply-pow [base exp]
  (apply * (repeat exp base)))

(= 16
   (non-acc-pow 2 4)
   (acc-pow 2 4)
   (lazy-pow 2 4)
   (iter-pow 2 4)
   (apply-pow 2 4))

Calling Java Under Cygwin

While trying to set up Clojure under Cygwin I found that doing mixed-mode between Cygwin and Java isn’t very happy due to the ‘;’ vs ‘:’ in the classpath.

This post (via this post) provided an obfuscated Ruby program to take care of that for you… thanks!

# Slightly obfuscated cygwin + windows java wrapper, automate cygpath

cpi = ARGV.index("-cp") + 1
cp = ARGV[cpi] if cpi

XBCP = "-Xbootclasspath/a:"

xbcpi = ARGV.index{|i|i=~/^#{XBCP}.*/}
xbcp = ARGV[xbcpi] if xbcpi

if cp or xbcpi
  def convert_paths(paths)
    paths = paths.gsub(':', ';').split(';')
    paths.map{|p|`cygpath -aw #{p}`.strip}.join ';'
  ARGV[cpi] = convert_paths(cp) if cp
  ARGV[xbcpi] = XBCP + convert_paths(xbcp.sub(XBCP, '')) if xbcp

java = '/cygdrive/c/Program Files/Java/jdk1.6.0_18/bin/java'
cmd = [java].concat ARGV

def e(s); "\"#{s.strip.gsub('"','\"')}\""; end

exec(cmd.map{|a|e a}.join(' '))

ParEdit for Editing Lispy Languages

ParEdit (paredit.el) is a minor mode for performing structured editing of S-expression data. The typical example of this would be Lisp or Scheme source code.

ParEdit helps keep parentheses balanced and adds many keys for moving S-expressions and moving around in S-expressions.

That quote from EmacsWiki really undersells Paredit, though.

Paredit makes it virtually impossible to un-balance parentheses (aka round, square, and curly brackets).

This mode would be especially interesting for folks avoiding Lisp because of the nightmare of balancing parentheses is too much of an obstacle to overcome (in practice of course it really isn’t, even if you don’t use Paredit).