verification - Can I mix post conditions and recursive functions in Clojure? -


is possible use both recur , post-condition functionality in same clojure function? hoping throw exception using post-condition, clojure appears trying wrap exception throwing code after recur somehow, (just stupid example) functions cannot evaluated.

(defn countup [x]   {:pre [(>= x 0)]    :post [(>= % 0)]}   (if (< x 1000000)     (recur (inc x))     x)) 

i'm using clojure 1.3 @ moment.

if @ implementation of defn @ https://github.com/clojure/clojure/blob/master/src/clj/clojure/core.clj#l3905 you'll see body of function gets modified tail calls pushed out of tail-position. 1 way around use auxiliary function call recur'd function , put post-condition on instead:

(defn- countup* [x]   (if (< x 1000000)     (recur (inc x))     x))  (defn countup [x]   {:pre [(>= x 0)]    :post [(>= % 0)]}   (countup* x))  (countup 999999) ;=> 1000000  (countup -1) ; assert failed: (>= x 0) 

Comments

Popular posts from this blog

android - Spacing between the stars of a rating bar? -

html - Instapaper-like algorithm -

c# - How to execute a particular part of code asynchronously in a class -