Skip to content

Commit

Permalink
Add q78
Browse files Browse the repository at this point in the history
  • Loading branch information
davidkun committed Sep 25, 2024
1 parent 318bef0 commit 547c409
Show file tree
Hide file tree
Showing 2 changed files with 26 additions and 0 deletions.
9 changes: 9 additions & 0 deletions src/ex/q078.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,9 @@
;; https://4clojure.oxal.org/#/problem/078

(ns ex.q078)

(defn ans
"Problem 78, Reimplement Trampoline"
[f & xs]
(loop [fn-or-val (apply f xs)]
(if (fn? fn-or-val) (recur (fn-or-val)) fn-or-val)))
17 changes: 17 additions & 0 deletions test/ex/q078_test.clj
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
;; https://4clojure.oxal.org/#/problem/078

(ns ex.q078-test
(:require [clojure.test :as t]
[ex.q078 :as sut]))

(t/deftest test-q78
(t/testing "Problem 78, Reimplement Trampoline"
(t/is (= 82
(letfn [(triple [x] #(sub-two (* 3 x)))
(sub-two [x] #(stop? (- x 2)))
(stop? [x] (if (> x 50) x #(triple x)))]
(sut/ans triple 2))))
(t/is (= [true false true false true false]
(letfn [(my-even? [x] (if (zero? x) true #(my-odd? (dec x))))
(my-odd? [x] (if (zero? x) false #(my-even? (dec x))))]
(map (partial sut/ans my-even?) (range 6)))))))

0 comments on commit 547c409

Please sign in to comment.