-
Notifications
You must be signed in to change notification settings - Fork 12
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #106 from Kodiologist/loop
Reimplement `loop`
- Loading branch information
Showing
5 changed files
with
88 additions
and
88 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,58 +1,64 @@ | ||
(require | ||
hyrule [loop]) | ||
(import | ||
math | ||
sys | ||
hyrule [inc dec]) | ||
hyrule [inc dec recur] | ||
pytest) | ||
|
||
|
||
(defn tco-sum [x y] | ||
(loop [[x x] [y y]] | ||
(cond | ||
(> y 0) (recur (inc x) (dec y)) | ||
(< y 0) (recur (dec x) (inc y)) | ||
True x))) | ||
(defn test-factorial [] | ||
(assert (= | ||
(loop [[i 5] [acc 1]] | ||
(if (= i 0) | ||
acc | ||
(recur (dec i) (* acc i)))) | ||
(math.factorial 5)))) | ||
|
||
|
||
(defn non-tco-sum [x y] | ||
(cond | ||
(> y 0) (inc (non-tco-sum x (dec y))) | ||
(< y 0) (dec (non-tco-sum x (inc y))) | ||
True x)) | ||
(defn test-tco-sum [] | ||
|
||
; This plain old tail-recursive function should exceed Python's | ||
; default maximum recursion depth. | ||
(defn non-tco-sum [x y] | ||
(cond | ||
(> y 0) (inc (non-tco-sum x (dec y))) | ||
(< y 0) (dec (non-tco-sum x (inc y))) | ||
True x)) | ||
(with [(pytest.raises RecursionError)] | ||
(non-tco-sum 100 10,000)) | ||
|
||
(defn test-loop [] | ||
;; non-tco-sum should fail | ||
(try | ||
(setv n (non-tco-sum 100 10000)) | ||
(except [e RuntimeError] | ||
(assert True)) | ||
(else | ||
(assert False))) | ||
; With `loop`, it should work. | ||
(defn tco-sum [x y] | ||
(loop [[x x] [y y]] | ||
(cond | ||
(> y 0) (recur (inc x) (dec y)) | ||
(< y 0) (recur (dec x) (inc y)) | ||
True x))) | ||
(assert (= (tco-sum 100 10,000) 10,100))) | ||
|
||
;; tco-sum should not fail | ||
(try | ||
(setv n (tco-sum 100 10000)) | ||
(except [e RuntimeError] | ||
(assert False)) | ||
(else | ||
(assert (= n 10100))))) | ||
|
||
(defn test-nested [] | ||
(assert (= | ||
(loop [[x 1]] | ||
(if (< x 3) | ||
(recur (+ x 1)) | ||
[x (loop [[y 1]] | ||
(if (< y 5) | ||
(recur (+ y 1)) | ||
y))])) | ||
[3 5]))) | ||
|
||
(defn test-recur-in-wrong-loc [] | ||
(defn bad-recur [n] | ||
(loop [[i n]] | ||
(if (= i 0) | ||
0 | ||
(inc (recur (dec i)))))) | ||
|
||
(try | ||
(bad-recur 3) | ||
(except [e TypeError] | ||
(assert True)) | ||
(else | ||
(assert False)))) | ||
(defn test-fancier-args [] | ||
(assert (= | ||
(loop [[x 1] #* a #** b] | ||
(if (= x 1) | ||
(recur 2 3 4 :foo "bar") | ||
[x a b])) | ||
[2 #(3 4) {"foo" "bar"}]))) | ||
|
||
|
||
(defn test-recur-string [] | ||
"test that `loop` doesn't touch a string named `recur`" | ||
"`loop` shouldn't touch a string named `recur`." | ||
(assert (= (loop [] (+ "recur" "1")) "recur1"))) |